<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-9-23
 * Time: 下午5:41
 */
class UserController extends Controller {

    private $_logininfo;
    private $_daytime;
    private $_daydate;
    private $_title;

    public function init() {
        parent::init();
        $this->_logininfo = $this->_session->offsetGet('loginInfos');
        if(!$this->_session->has('loginInfos')) {
            $this->redirect('/login');
            die();
        }
        $__now_time = $_SERVER['REQUEST_TIME'];
        $this->_nowtime = date("Y-m-d H:i:s",$__now_time);
        $this->_nowdate = date("Y-m-d",$__now_time);
        // 取得用户信息
        $user_info = $this->_session->offsetGet ( 'loginInfos' );
        $this->_view->userInfo=$user_info;
    }

    //会员中心
    public function indexAction(){
        global $_SupplyOrderCfg,$_getAreaNames,$_PurchaseOrderCfg,$_ServiceCfg;;
        $this->_title ="会员中心";
        $this->_keywords = '会员中心';
        $this->_description = '会员中心';
        $this->_view->title = $this->_title;
        $this->_view->keywords = $this->_keywords;
        $this->_view->description = $this->_description;
        $kd=empty($_GET['kd']) ? '' :htmlspecialchars(trim($_GET['kd']) , ENT_QUOTES ,'utf-8');
        define('ITEM_PER_PAGE', 10);
        $index = new IndexModel();
        $cate = $index->getCateInfo("cate",'','');
        //分割日期
        $time=date('Y-m-d',time());
        $date=explode('-',$time);
        $this->_view->date=$date;
        //获取用户信息
        $user = new UserModel();
        $where = "users.id = '{$this->_logininfo['id']}'";//查询默认公司
        $userinfo = $user->getUserInfoByWhere($where,1);
        //求购订单总数
        $order = new SaleorderModel();
        $where_all = "sale_order_business.buy_user_id = {$this->_logininfo['id']} AND sale_order.status in({$_PurchaseOrderCfg['audit']},{$_PurchaseOrderCfg['deal']},{$_PurchaseOrderCfg['done']},{$_PurchaseOrderCfg['cancel']})";
        $buycount =  $order->getBuyOrderCountByWhere($where_all);
        //供应订单总数
        $order = new SaleorderModel();
        $where_all = "sale_order_business.sale_user_id = {$this->_logininfo['id']} AND sale_order.status in({$_SupplyOrderCfg['audit']},{$_SupplyOrderCfg['deal']},{$_SupplyOrderCfg['done']},{$_SupplyOrderCfg['cancel']})";
        $salecount =  $order->getSaleOrderCountByWhere($where_all);
        //四条求购显示
        $buy=new BuyModel();
        $buylist=$buy->getuserList();
//        var_dump($buylist);exit;
        $this->_view->buylist=$buylist;
        $this->_view->salecount=$salecount;
        $this->_view->buycount=$buycount;
        $this->_view->cate = $cate;
        $this->_view->kd = $kd;
        $this->_view->userinfo = $userinfo;
        $this->_view->_getAreaNames = $_getAreaNames;
    }

    //销售单--供应订单
    public function saleorderAction(){
        global $_SupplyOrderMean,$_SupplyOrderCfg,$_orderDeliveryTypeMean,$_orderDeliveryTypeCfg,$_getAreaNames;
        define ( 'ITEM_PER_PAGE', 5 );
        $this->_title ="我的供应订单";
        $this->_keywords = '我的供应订单';
        $this->_description = '我的供应订单';
        $this->_view->title = $this->_title;
        $this->_view->keywords = $this->_keywords;
        $this->_view->description = $this->_description;
        $kd=empty($_GET['kd']) ? '' :htmlspecialchars(trim($_GET['kd']) , ENT_QUOTES ,'utf-8');
        $page=empty($_GET['page']) ? '' :htmlspecialchars(trim($_GET['page']) , ENT_QUOTES ,'utf-8');
        $url = "/user/saleorder";
        $index = new IndexModel();
        $cate = $index->getCateInfo("cate",'','');
        $order = new SaleorderModel();
        //这边获取查询条件
        $cate_name=empty($_GET['cate_name']) ? '' :htmlspecialchars(trim($_GET['cate_name']) , ENT_QUOTES ,'utf-8');
        $product_name=empty($_GET['product_name']) ? '' :htmlspecialchars(trim($_GET['product_name']) , ENT_QUOTES ,'utf-8');
        $brand_name=empty($_GET['brand_name']) ? '' :htmlspecialchars(trim($_GET['brand_name']) , ENT_QUOTES ,'utf-8');
        $delivery_area=empty($_GET['delivery_area']) ? '' :htmlspecialchars(trim($_GET['delivery_area']) , ENT_QUOTES ,'utf-8');
        $status=empty($_GET['status']) ? '' :htmlspecialchars(trim($_GET['status']) , ENT_QUOTES ,'utf-8');
        $condition = array();
        $urls=array();
        //导航状态的总数
        $where_all = "sale_order_business.sale_user_id = {$this->_logininfo['id']} AND sale_order.status in({$_SupplyOrderCfg['audit']},{$_SupplyOrderCfg['deal']},{$_SupplyOrderCfg['done']},{$_SupplyOrderCfg['cancel']})";
        $totalItemsall =  $order->getSaleOrderCountByWhere($where_all);

        $where_audit = "sale_order_business.sale_user_id = {$this->_logininfo['id']} AND sale_order.status in({$_SupplyOrderCfg['audit']})";
        $totalItemsaudit =  $order->getSaleOrderCountByWhere($where_audit);

        $where_deal = "sale_order_business.sale_user_id = {$this->_logininfo['id']} AND sale_order.status in({$_SupplyOrderCfg['deal']})";
        $totalItemsdeal =  $order->getSaleOrderCountByWhere($where_deal);

        $where_done = "sale_order_business.sale_user_id = {$this->_logininfo['id']} AND sale_order.status in({$_SupplyOrderCfg['done']})";
        $totalItemsdone =  $order->getSaleOrderCountByWhere($where_done);

        $where_cancel = "sale_order_business.sale_user_id = {$this->_logininfo['id']} AND sale_order.status in({$_SupplyOrderCfg['cancel']})";
        $totalItemscancel =  $order->getSaleOrderCountByWhere($where_cancel);

        if(!empty($cate_name)){
            $condition[] = "product_cates.cate_name like '%{$cate_name}%'";
            $urls[]="cate_name={$cate_name}";
        }

        if(!empty($product_name)){
            $condition[] = "product.product_name like '%{$product_name}%'";
            $urls[]="product_name={$product_name}";
        }

        if(!empty($brand_name)){
            $condition[] = "product_brands.brand_name like '%{$brand_name}%'";
            $urls[]="brand_name={$brand_name}";
        }

        if(!empty($delivery_area)){
            $condition[] = "sale_order_sku.delivery_area like '%{$delivery_area}%'";
            $urls[]="delivery_area={$delivery_area}";
        }

        if(!empty($status)){
            $condition[] = "sale_order.status = '{$status}'";
            $urls[]="status={$status}";
        }
        $where = "1=1";
        if(empty($condition))
        {
            $where  .= " AND sale_order_business.sale_user_id = {$this->_logininfo['id']}";
        } else {
            $where  .=" AND sale_order_business.sale_user_id = {$this->_logininfo['id']}". "  AND ".implode(' AND ', $condition);
        }

        $url .= "?page=%d";
        if(!empty($urls)){
            $url.=  "&".implode('&',$urls);
        }
        $orderby = "sale_order.create_time desc";

        $groupby = "";

        $page = empty($page)? 1 :(int)$page;

        //这边开始
        $totalItems =  $order->getSaleOrderCountByWhere($where);
        $offset = 0;
        $page =  empty($page) ? 1 :$page;
        $offset = ($page - 1) * ITEM_PER_PAGE;
        if ($offset >= $totalItems) {
            $page = ceil ( $totalItems / ITEM_PER_PAGE );
            $offset = ($page - 1) * ITEM_PER_PAGE;
        }
        if ($offset < 0) {
            $offset = 0;
            $page = 1;
        }

        $data = $order->getSaleOrderListByWhere($where,ITEM_PER_PAGE,$offset,$orderby,$groupby);
//        var_dump($data);die();
        $links = Pagination::createPage($url, ITEM_PER_PAGE, $page, $totalItems, 2 ,'_self',true);



        $this->_view->cate = $cate;
        $this->_view->kd = $kd;
        $this->_view->data = $data;
        $this->_view->links = $links;

        $this->_view->_SupplyOrderMean = $_SupplyOrderMean;
        $this->_view->_SupplyOrderCfg = $_SupplyOrderCfg;
        $this->_view->_orderDeliveryTypeMean = $_orderDeliveryTypeMean;
        $this->_view->_orderDeliveryTypeCfg = $_orderDeliveryTypeCfg;

        $this->_view->cate_name = $cate_name;
        $this->_view->product_name = $product_name;
        $this->_view->brand_name = $brand_name;
        $this->_view->delivery_area = $delivery_area;
        $this->_view->status = $status;

        $this->_view->totalItemsall = $totalItemsall;
        $this->_view->totalItemsaudit = $totalItemsaudit;
        $this->_view->totalItemsdeal = $totalItemsdeal;
        $this->_view->totalItemsdone = $totalItemsdone;
        $this->_view->totalItemscancel = $totalItemscancel;
        $this->_view->_getAreaNames = $_getAreaNames;

    }

    //修改订单信息
    public function editsaleorderAction(){
        if($this->_request->isPost()) {
            $Post = $this->_request->getPost();
            $Post['order_id'] = !empty($Post['order_id'])? (int)trim(htmlspecialchars($Post['order_id'],ENT_QUOTES,"UTF-8")):'';
            $Post['order_sku_id'] = !empty($Post['order_sku_id'])? (int)trim(htmlspecialchars($Post['order_sku_id'],ENT_QUOTES,"UTF-8")):'';
            $Post['price'] = !empty($Post['price'])? (int)trim(htmlspecialchars($Post['price'],ENT_QUOTES,"UTF-8")):'';
            $Post['num'] = !empty($Post['num'])? trim(htmlspecialchars($Post['num'],ENT_QUOTES,"UTF-8")):'';
            $Post['delivery_method'] = !empty($Post['delivery_method'])? trim(htmlspecialchars($Post['delivery_method'],ENT_QUOTES,"UTF-8")):'';
            $Post['delivery_addr'] = !empty($Post['delivery_addr'])? trim(htmlspecialchars($Post['delivery_addr'],ENT_QUOTES,"UTF-8")):'';
            $user = new UserModel();
            $data = array(
                'update_uid'=>$this->_logininfo['id'],
                'update_time'=>$this->_nowtime,
                'price'=>$Post['price'],
                'num'=>$Post['num'],
                'amount'=>$Post['num']*$Post['price'],
                'delivery_method'=>$Post['delivery_method'],
                'delivery_addr'=>$Post['delivery_addr'],
            );

           if($user->editsaleorder($data,$Post['order_sku_id'],$Post['order_id'])){
                die("1");
           }else{
                die("2");
           }
            die();
        }
    }
    //修改订单状态
    public function setOrderStatusAction(){
        global $_SupplyOrderCfg;
        $status=empty($_GET['status']) ? '' :(int)htmlspecialchars(trim($_GET['status']) , ENT_QUOTES ,'utf-8');
        $order_id=empty($_GET['order_id']) ? '' :(int)htmlspecialchars(trim($_GET['order_id']) , ENT_QUOTES ,'utf-8');
        if(empty($status)){
            $this->redirect ( '/error/error404?error=订单状态获取失败' );
            die();
        }else{
            if($status>$_SupplyOrderCfg['cancel']){
                $this->redirect ( '/error/error404?error=订单状态操作不正确' );
                die();
            }
        }
        if(empty($order_id)){
            $this->redirect ( '/error/error404?error=订单信息获取失败' );
            die();
        }
        $user = new UserModel();

        $order = new SaleorderModel();
        //这边检查订单是否存在
        $order_info = $order->getListById($order_id);
        if(empty($order_info)){
            $this->redirect ( '/error/error404?error=订单信息获取失败' );
            die();
        }
        //修改状态
        if($user->setOrderStatus($status,$order_id)){
            //这边如果是取消订单的，要返回数量
            $orderinfo = $user->getOrderInfoById($order_id);
            $this->_db_handler->update("iq_quotation",array('num'=>$orderinfo['quote_num']+$orderinfo['num']),"id = '{$orderinfo['buy_sku_id']}'");
//            dieJs("");
            Tools::alertBack("操作成功");
            $this->redirect ( '/user/saleorder');
            die();
        }else{
            $this->redirect ( '/error/error404?error=操作失败' );
            die();
        }
    }

    //采购单--求购订单
    public function buyorderAction(){
        global $_PurchaseOrderMean,$_PurchaseOrderCfg,$_saleOrderType,$_getAreaNames,$_orderDeliveryTypeMean,$_orderDeliveryTypeCfg;
        define ( 'ITEM_PER_PAGE', 5 );
        $this->_title ="我的采购订单";
        $this->_keywords = '我的采购订单';
        $this->_description = '我的采购订单';
        $this->_view->title = $this->_title;
        $this->_view->keywords = $this->_keywords;
        $this->_view->description = $this->_description;
        $kd=empty($_GET['kd']) ? '' :htmlspecialchars(trim($_GET['kd']) , ENT_QUOTES ,'utf-8');
        $page=empty($_GET['page']) ? '' :htmlspecialchars(trim($_GET['page']) , ENT_QUOTES ,'utf-8');
        $url = "/user/buyorder";
        $index = new IndexModel();
        $cate = $index->getCateInfo("cate",'','');


        $order = new SaleorderModel();
        //这边获取查询条件
        $cate_name=empty($_GET['cate_name']) ? '' :htmlspecialchars(trim($_GET['cate_name']) , ENT_QUOTES ,'utf-8');
        $product_name=empty($_GET['product_name']) ? '' :htmlspecialchars(trim($_GET['product_name']) , ENT_QUOTES ,'utf-8');
        $brand_name=empty($_GET['brand_name']) ? '' :htmlspecialchars(trim($_GET['brand_name']) , ENT_QUOTES ,'utf-8');
        $delivery_area=empty($_GET['delivery_area']) ? '' :htmlspecialchars(trim($_GET['delivery_area']) , ENT_QUOTES ,'utf-8');
        $status=empty($_GET['status']) ? '' :htmlspecialchars(trim($_GET['status']) , ENT_QUOTES ,'utf-8');
        $condition = array();
        $urls=array();
        //导航状态的总数
        $where_all = "sale_order_business.buy_user_id = {$this->_logininfo['id']} AND sale_order.status in({$_PurchaseOrderCfg['audit']},{$_PurchaseOrderCfg['deal']},{$_PurchaseOrderCfg['done']},{$_PurchaseOrderCfg['cancel']})";
        $totalItemsall =  $order->getBuyOrderCountByWhere($where_all);

        $where_audit = "sale_order_business.buy_user_id = {$this->_logininfo['id']} AND sale_order.status in({$_PurchaseOrderCfg['audit']})";
        $totalItemsaudit =  $order->getBuyOrderCountByWhere($where_audit);

        $where_deal = "sale_order_business.buy_user_id = {$this->_logininfo['id']} AND sale_order.status in({$_PurchaseOrderCfg['deal']})";
        $totalItemsdeal =  $order->getBuyOrderCountByWhere($where_deal);

        $where_done = "sale_order_business.buy_user_id = {$this->_logininfo['id']} AND sale_order.status in({$_PurchaseOrderCfg['done']})";
        $totalItemsdone =  $order->getBuyOrderCountByWhere($where_done);

        $where_cancel = "sale_order_business.buy_user_id = {$this->_logininfo['id']} AND sale_order.status in({$_PurchaseOrderCfg['cancel']})";
        $totalItemscancel =  $order->getBuyOrderCountByWhere($where_cancel);

        if(!empty($cate_name)){
            $condition[] = "product_cates.cate_name like '%{$cate_name}%'";
            $urls[]="cate_name={$cate_name}";
        }

        if(!empty($product_name)){
            $condition[] = "product.product_name like '%{$product_name}%'";
            $urls[]="product_name={$product_name}";
        }

        if(!empty($brand_name)){
            $condition[] = "product_brands.brand_name like '%{$brand_name}%'";
            $urls[]="brand_name={$brand_name}";
        }

        if(!empty($delivery_area)){
            $condition[] = "sale_order_sku.delivery_area like '%{$delivery_area}%'";
            $urls[]="delivery_area={$delivery_area}";
        }

        if(!empty($status)){
            $condition[] = "sale_order.status = '{$status}'";
            $urls[]="status={$status}";
        }
        $where = "1=1";
        if(empty($condition))
        {
            $where  .= " AND sale_order_business.buy_user_id = {$this->_logininfo['id']}";
        } else {
            $where  .=" AND sale_order_business.buy_user_id = {$this->_logininfo['id']}". "  AND ".implode(' AND ', $condition);
        }

        $url .= "?page=%d";
        if(!empty($urls)){
            $url.=  "&".implode('&',$urls);
        }
        $orderby = "sale_order.create_time desc";

        $groupby = "";

        $page = empty($page)? 1 :(int)$page;

        //这边开始
        $totalItems =  $order->getBuyOrderCountByWhere($where);
        $offset = 0;
        $page =  empty($page) ? 1 :$page;
        $offset = ($page - 1) * ITEM_PER_PAGE;
        if ($offset >= $totalItems) {
            $page = ceil ( $totalItems / ITEM_PER_PAGE );
            $offset = ($page - 1) * ITEM_PER_PAGE;
        }
        if ($offset < 0) {
            $offset = 0;
            $page = 1;
        }

        $data = $order->getBuyOrderListByWhere($where,ITEM_PER_PAGE,$offset,$orderby,$groupby);
//        var_dump($data);die();
        $links = Pagination::createPage($url, ITEM_PER_PAGE, $page, $totalItems, 2 ,'_self',true);



        $this->_view->cate = $cate;
        $this->_view->kd = $kd;
        $this->_view->data = $data;
        $this->_view->links = $links;

        $this->_view->_PurchaseOrderMean = $_PurchaseOrderMean;
        $this->_view->_PurchaseOrderCfg = $_PurchaseOrderCfg;
        $this->_view->_orderDeliveryTypeMean = $_orderDeliveryTypeMean;
        $this->_view->_orderDeliveryTypeCfg = $_orderDeliveryTypeCfg;

        $this->_view->cate_name = $cate_name;
        $this->_view->product_name = $product_name;
        $this->_view->brand_name = $brand_name;
        $this->_view->delivery_area = $delivery_area;
        $this->_view->status = $status;

        $this->_view->totalItemsall = $totalItemsall;
        $this->_view->totalItemsaudit = $totalItemsaudit;
        $this->_view->totalItemsdeal = $totalItemsdeal;
        $this->_view->totalItemsdone = $totalItemsdone;
        $this->_view->totalItemscancel = $totalItemscancel;
        $this->_view->_getAreaNames = $_getAreaNames;

    }
    //订单详情

    public function orderdetailAction(){
        global $_getAreaNames,$_PurchaseOrderMean,$_PurchaseOrderCfg,$_saleOrderType,$_orderDeliveryTypeMean,$_orderDeliveryTypeCfg;
        define ( 'ITEM_PER_PAGE', 5 );
        $this->_title ="订单详情";
        $this->_keywords = '订单详情';
        $this->_description = '订单详情';
        $this->_view->title = $this->_title;
        $this->_view->keywords = $this->_keywords;
        $this->_view->description = $this->_description;
        $kd=empty($_GET['kd']) ? '' :htmlspecialchars(trim($_GET['kd']) , ENT_QUOTES ,'utf-8');
        $order_id=empty($_GET['order_id']) ? '' :(int)htmlspecialchars(trim($_GET['order_id']) , ENT_QUOTES ,'utf-8');
        $index = new IndexModel();
        $cate = $index->getCateInfo("cate",'','');
        if(empty($order_id)){
            $this->redirect('error/error404');
            die();
        }
        $order = new SaleorderModel();
        //获取订单详情

        $order_info = $order->getOrderDetailInfo("sale_order.id = '{$order_id}'",'r');
        if(empty($order_info)){
            $this->redirect('error/error404');
            die();
        }

        $this->_view->cate = $cate;
        $this->_view->kd = $kd;
        $this->_view->order_info = $order_info;
        $this->_view->_getAreaNames = $_getAreaNames;
    }

    //批量上传报价
    public  function batchpublishquoteAction(){
        global $_getAreaNames;
        $this->_title ="批量上传报价";
        $this->_keywords = '批量上传报价';
        $this->_description = '批量上传报价';
        $this->_view->title = $this->_title;
        $this->_view->keywords = $this->_keywords;
        $this->_view->description = $this->_description;


        $kd=empty($_GET['kd']) ? '' :htmlspecialchars(trim($_GET['kd']) , ENT_QUOTES ,'utf-8');

        $index = new IndexModel();
        $cate = $index->getCateInfo("cate",'','');


        $this->_view->cate = $cate;
        $this->_view->kd = $kd;
        $this->_view->_getAreaNames = $_getAreaNames;
    }
    //接受上传文件报价

    public function uploadexcelAction(){
        global $_suppliyStateCfg,$_suppliyStateMean,$_settlement,$_orderDeliveryTypeMean,$_orderDeliveryTypeCfg;
        if($this->_request->isPost()) {
            $Post = $this->_request->getPost();
            $excel = $_FILES ['excel'] ['tmp_name'];
            $PHPReader = new PHPExcel_Reader_Excel5();//2003-2007
            if ($PHPReader->canRead ($excel)) {
                $phpExcel = $PHPReader->load ($excel);
            }else{
                $PHPReader1 =  new PHPExcel_Reader_Excel2007();//2010
                if($PHPReader1->canRead ($excel)){
                    $phpExcel = $PHPReader1->load ($excel);
                }
            }

            // 设置为第一页
            $phpExcel->setActiveSheetIndex ( 0 );
            // 取得最大行数
            $rowMax = $phpExcel->getActiveSheet ()->getHighestDataRow () - 1;

            // 最大列数
            $colMax = 12;//可根据所需的字段修改
            // 取得导入的数据
            $importData = array ();
            $A = ord ( "A" );
            $row = 2;
            for($i = 0; $i < $rowMax; $i ++) {
                $column = chr ( $A );
                for($j = 0; $j < $colMax; $j ++) {
                    $importData [$row] [$column] = $phpExcel->getActiveSheet ()->getCell ( $column . $row )->getValue ();
                    $column ++;
                }
                $row ++;
            }

            if(empty($importData)){
                die("100");
            }
            if(!empty($importData)){
                if(count($importData[2])<12){
                    die("101");//这表明表格模板不正确
                }
                foreach ( $importData as $key => $value ) {
                    $business_name = !empty($value['A'])? trim(htmlspecialchars($value['A'],ENT_QUOTES,"UTF-8")):'';
                    $cate_name = !empty($value['B'])? trim(htmlspecialchars($value['B'],ENT_QUOTES,"UTF-8")):'';
                    $product_name=!empty($value['C'])? trim(htmlspecialchars($value['C'],ENT_QUOTES,"UTF-8")):'';
                    $brand_name=!empty($value['D'])? trim(htmlspecialchars($value['D'],ENT_QUOTES,"UTF-8")):'';
                    $service_name=!empty($value['E'])? trim(htmlspecialchars($value['E'],ENT_QUOTES,"UTF-8")):'';
                    $price=!empty($value['F'])? trim(htmlspecialchars($value['F'],ENT_QUOTES,"UTF-8")):'';
                    $currency_name=!empty($value['G'])? trim(htmlspecialchars($value['G'],ENT_QUOTES,"UTF-8")):'';
                    $_settlement =!empty($value['H'])? trim(htmlspecialchars($value['H'],ENT_QUOTES,"UTF-8")):'';
                    $num=!empty($value['I'])? trim(htmlspecialchars($value['I'],ENT_QUOTES,"UTF-8")):'';
                    $delivery_method=!empty($value['J'])? trim(htmlspecialchars($value['J'],ENT_QUOTES,"UTF-8")):'';
                    $area_name=!empty($value['K'])? trim(htmlspecialchars($value['K'],ENT_QUOTES,"UTF-8")):'';
//                    $delivery_area=!empty($value['L'])? trim(htmlspecialchars($value['L'],ENT_QUOTES,"UTF-8")):'';
                    $delivery_time=!empty($value['L'])? trim(htmlspecialchars($value['L'],ENT_QUOTES,"UTF-8")):'';
                    //公司信息


                    //报价信息
                    $quotation_data[$key] = array(
                        'create_uid'=>$this->_logininfo['id'],
                        'create_time'=>$this->_nowtime,
                        'update_time'=>$this->_nowtime,
                        'update_uid'=>$this->_logininfo['id'],
                        'user_id'=>$this->_logininfo['id'],
                        'business_id'=>"",
                        'organization_id'=>"1",
                        'department_id'=>"1",
                        'type'=>'outer',
                        'status'=>$_suppliyStateCfg['audit'],
                        'product_id'=>"",
                        'service_id'=>"",
                        'currency_id'=>"",
                        'price'=>str_replace(' ','',$price),
                        'settlement'=>str_replace(' ','',$_settlement),
                        'num'=>str_replace(' ','',$num),
//                        'delivery_area'=>str_replace(' ','',$delivery_area),
                        'delivery_time'=>str_replace(' ','',$delivery_time),
                        'delivery_method'=>str_replace(' ','',$delivery_method),
                        'area_id'=>'',

                        'deposit_rate'=>"",
                        'tray_type'=>"",
                        'business_name' => str_replace(' ','',$business_name),
                        'cate_name'=>str_replace(' ','',$cate_name),
                        'product_name'=>str_replace(' ','',$product_name),
                        'brand_name'=>str_replace(' ','',$brand_name),
                        'service_name'=>str_replace(' ','',$service_name),
                        'currency_name'=>str_replace(' ','',$currency_name),
                        'area_name'=>str_replace(' ','',$area_name),
                        'rebate_price'=>"0",
                        'vice_uom_id'=>"",
                        'vice_rate'=>"",
                        'buy_qty'=>"0",
                    );



                }
            }
//var_dump($quotation_data);die();
            //这边要检测判断数据的完整性和有效性
            $arr = array();
            if(!empty($quotation_data)){
                foreach($quotation_data as $k=>$v){
                    //查询公司的id
                    if(empty($v['business_name'])){
                        $arr[$k]['business_name'] = '公司名称为空';

                    }else{
                        $business = new UsersinvoiceModel();
                        $business_info = $business->getUserBusinessyName("business_name= '{$v['business_name']}' and user_id = '{$this->_logininfo['id']}'");//这边的是当前的user的公司
                        if(!empty($business_info)){
                            $quotation_data[$k]['business_id'] = $business_info['business_id'];
                        }else{
                            //这边就没有填写的公司名称，让客户先去添加上公司
                            $arr[$k]['business_name'] = '所填公司名称没有创建';
                        }
                    }


                    //查询产品的id
                    if(!empty($v['product_name'])&&!empty($v['brand_name'])&&!empty($v['cate_name'])){
                        $product = new ProductModel();
                        //这边创建产品
                        $product_id = $product->getProdutByName($v['product_name'],$v['brand_name'],$v['cate_name']);
                        if(!$product_id){
                            //这边插入产品,
                            $product_id = $product->addProdutByName($v['product_name'],$v['brand_name'],$v['cate_name']);
                        }
                        $quotation_data[$k]['product_id'] = $product_id;
                    }else{
                        $arr[$k]['product'] = '所填产品信息不全';
                    }

                    //查询service_id
                    if(empty($v['service_name'])){
                        $arr[$k]['service_name'] = '交易类型为空';
                    }else{
                        $com = new CommonModel();
                        $service_info = $com->getService("service_name= '{$v['service_name']}'" );
                        if(!empty($service_info)){
                            $quotation_data[$k]['service_id'] = $service_info['id'];
                        }else{
                            //这边填写的交易类型不正确
                            $arr[$k]['service_name'] = '交易类型填写不正确';
                        }
                    }
                    //判断价格
                    if(empty($v['price'])){
                        $arr[$k]['price'] = '价格不能为空';
                    }

                    //查询currency_id
                    if(empty($v['currency_name'])){
                        $arr[$k]['currency_name'] ='币种不能为空';
                    }else{
                        $com = new CommonModel();
                        $currency_info = $com->getCurrency("name= '{$v['currency_name']}'" );
                        if(!empty($currency_info)){
                            $quotation_data[$k]['currency_id'] = $currency_info['id'];
                            //这边判断美金要填写结算方式
                            if($v['currency_name']=="美金"){
                                if(empty($v['settlement'])){
                                    $arr[$k]['settlement'] = '美金结算方式不能为空';
                                }else{
                                    if(!in_array($v['settlement'],$_settlement)){
                                        $arr[$k]['settlement'] = '美金结算方式填写不正确';
                                    }
                                }
                            }
                        }else{
                            //这边填写的币种不正确
                            $arr[$k]['currency_name'] = '币种填写不正确';
                        }
                    }
                    //判断数量
                    if(empty($v['num'])){
                        $arr[$k]['num'] = '数量不能为空';
                    }
                    //判断提货方式
                    if(empty($v['delivery_method'])){
                        $arr[$k]['delivery_method'] = '提货方式不能为空';
                    }else{
                        $method = array('自提','配送');
                        if(!in_array($v['delivery_method'],$method)){
                            $arr[$k]['delivery_method'] = '提货方式填写不正确';
                        }else{
                            if($v['delivery_method']=='配送'){
                                $quotation_data[$k]['delivery_method']=='2';
                            }else{
                                $quotation_data[$k]['delivery_method']=='1';
                            }
                        }
                    }

                    //这边的填写交货地点的会一直存在个问题，交货地点不按照正规的地点填写，就没有这样的地点则这个交货地点为空
                    //查询area_id
                    if(empty($v['area_name'])){
                        $arr[$k]['area_name'] = '交货地点不能为空';
                    }else{
                        //这边根据模糊搜索查询得到地点
                        $area_info = $com->getArea("region_name like '%{$v['area_name']}%'");
                        if(!empty($area_info)){
                            $quotation_data[$k]['area_id'] = $area_info['region_id'];
                        }else{
                            //这边填写的地区不存在，这边我不给报错了，否则他们一直报价会不成功，只要他们的交货地点不按照正规的格式填写就会错误
//                            $arr[$k]['area_name'] = '交货地点填写不合理';
                            $quotation_data[$k]['area_id'] = "0";
                        }
                    }

//                    //判断交货地点
//                    if(empty($v['delivery_area'])){
//                        $arr[$k]['delivery_area'] = '交货地点不能为空';
//                    }

                    //判断交货时间
                    if(empty($v['delivery_time'])){
                        $arr[$k]['delivery_time'] = '交货时间不能为空';
                    }
                    //////////////

                }
            }
            //去掉没用的字段
            if(!empty($quotation_data)){
                foreach($quotation_data as $key=>$val){
                    unset($quotation_data[$key]['business_name']);
                    unset($quotation_data[$key]['cate_name']);
                    unset($quotation_data[$key]['product_name']);
                    unset($quotation_data[$key]['brand_name']);
                    unset($quotation_data[$key]['service_name']);
                    unset($quotation_data[$key]['currency_name']);
                    unset($quotation_data[$key]['area_name']);
                }
            }

            //这边也要去判断重复的数据
            if(empty($arr)){
                //没错就表示通过
                if(!empty($quotation_data)){
                    foreach($quotation_data as $keys=>$row){
                        if($keys>2){
                            $arrs = array_diff($quotation_data[$keys-1],$quotation_data[$keys]);
                            if(empty($arrs)){
                                unset($quotation_data[$keys-1]);
                            }
                        }
                    }
                }
            }

            $tip = array();
            //下面开始插入数据库
            if(!empty($arr)){
                $tip = array(
                    'code'=>'1001',
                    'list'=>$arr
                );
                echo json_encode($tip);die();
            }else{
                //这边开始存数据
                $user = new UserModel();
                //这边要查询是否重复

                $res = $user->savebatchquote($quotation_data);
                //这边更新user_invoice 中 quotation_update_time
                if($res && $res>0){
                    $tip = array(
                        'code'=>'1008',
                        'list'=>''
                    );
                    echo json_encode($tip);die();
                }
            }
            die();
        }
    }

    //单条发布报价

    public function publishquoteAction(){
        global $_getAreaNames,$_getAreaname;
        $this->_title ="单条上传报价";
        $this->_keywords = '单条上传报价';
        $this->_description = '单条上传报价';
        $this->_view->title = $this->_title;
        $this->_view->keywords = $this->_keywords;
        $this->_view->description = $this->_description;
        $kd=empty($_GET['kd']) ? '' :htmlspecialchars(trim($_GET['kd']) , ENT_QUOTES ,'utf-8');
        $index = new IndexModel();
        $cate = $index->getCateInfo("cate",'','');

        //这边查询报价的公司
        $business = new UsersinvoiceModel();
        $business_info = $business->getUserInfoById($this->_logininfo['id'],'','a');
        $com = new CommonModel();
        $service_info = $com->getService_info("1=1");

        $this->_view->cate = $cate;
        $this->_view->kd = $kd;
        $this->_view->business_info = $business_info;
        $this->_view->service_info = $service_info;
        $this->_view->_getAreaNames = $_getAreaNames;
        $this->_view->_getAreaname = $_getAreaname;
    }

    //求购报价
    public function quotelistAction(){
        global $_suppliyStateMean,$_getAreaNames;
        define ( 'ITEM_PER_PAGE', 10 );
        $this->_title ="求购报价";
        $this->_keywords = '求购报价';
        $this->_description = '求购报价';
        $this->_view->title = $this->_title;
        $this->_view->keywords = $this->_keywords;
        $this->_view->description = $this->_description;
        $page=empty($_GET['page']) ? 1 :htmlspecialchars(trim($_GET['page']) , ENT_QUOTES ,'utf-8');
        $kd=empty($_GET['kd']) ? '' :htmlspecialchars(trim($_GET['kd']) , ENT_QUOTES ,'utf-8');
        $url = "/user/quotelist";
        $index = new IndexModel();
        $cate = $index->getCateInfo("cate",'','');
        //这边获取公司的id---默认的
        $quote = new QuotationModel();

        $business = new UsersinvoiceModel();
        $business_info = $business->getUserInfoById($this->_logininfo['id'],'Y','r');
        $com = new CommonModel();
        $service_info = $com->getService_info("1=1");

        //获取查询参数
        $cate_name=empty($_GET['cate_name']) ? '' :htmlspecialchars(trim($_GET['cate_name']) , ENT_QUOTES ,'utf-8');

        $brand_name=empty($_GET['brand_name']) ? '' :htmlspecialchars(trim($_GET['brand_name']) , ENT_QUOTES ,'utf-8');

        $product_name=empty($_GET['product_name']) ? '' :htmlspecialchars(trim($_GET['product_name']) , ENT_QUOTES ,'utf-8');
        $condition = array();
        $urls=array();
        if(!empty($cate_name)){
            $condition[] = "product_cates.cate_name like '%{$cate_name}%'";
            $urls[]="cate_name={$cate_name}";
        }

        if(!empty($product_name)){
            $condition[] = "product.product_name like '%{$product_name}%'";
            $urls[]="product_name={$product_name}";
        }

        if(!empty($brand_name)){
            $condition[] = "product_brands.brand_name like '%{$brand_name}%'";
            $urls[]="brand_name={$brand_name}";
        }
        $where = "1=1";
        if(empty($condition))
        {
            $where  .= " AND iq_quotation.user_id = '{$this->_logininfo['id']}' and iq_quotation.type = 'require'";
        } else {
            $where  .=" AND iq_quotation.user_id = '{$this->_logininfo['id']}' and iq_quotation.type = 'require'". "  AND ".implode(' AND ', $condition);
        }
        $url .= "?page=%d";
        if(!empty($urls)){
            $url.=  "&".implode('&',$urls);
        }

        $order = "iq_quotation.create_time desc";
        $group = "";

        //这边开始
        $totalItems =  $quote->getQuotesCountByWhere($where);
        $offset = 0;
        $page =  empty($page) ? 1 :(int)$page;
        $offset = ($page - 1) * ITEM_PER_PAGE;
        if ($offset >= $totalItems) {
            $page = ceil ( $totalItems / ITEM_PER_PAGE );
            $offset = ($page - 1) * ITEM_PER_PAGE;
        }
        if ($offset < 0) {
            $offset = 0;
            $page = 1;
        }

        $data = $quote->getQuotesListByWhere($where,ITEM_PER_PAGE,$offset,$order,$group);
//        var_dump($data);
        $links = Pagination::createPage($url, ITEM_PER_PAGE, $page, $totalItems, 2 ,'_self',true);
        $this->_view->cate = $cate;
        $this->_view->kd = $kd;
        $this->_view->business_info = $business_info;
        $this->_view->service_info = $service_info;
        $this->_view->links = $links;
        $this->_view->data = $data;
        $this->_view->_suppliyStateMean = $_suppliyStateMean;
        $this->_view->count = $totalItems;
        $this->_view->cate_name = $cate_name;
        $this->_view->brand_name = $brand_name;
        $this->_view->product_name = $product_name;
        $this->_view->_getAreaNames = $_getAreaNames;
    }


    //接收提交的数据
    public function savepublishquoteAction(){
        global $_suppliyStateCfg,$_suppliyStateMean,$_settlement,$_orderDeliveryTypeMean,$_orderDeliveryTypeCfg;
        if($this->_request->isPost()) {
            $Post = $this->_request->getPost();
            $Post['product_id'] = !empty($Post['product_id'])? trim(htmlspecialchars($Post['product_id'],ENT_QUOTES,"UTF-8")):'';
            $Post['business_id'] = !empty($Post['business_id'])? trim(htmlspecialchars($Post['business_id'],ENT_QUOTES,"UTF-8")):'';
            $Post['cate_name'] = !empty($Post['cate_name'])? trim(htmlspecialchars($Post['cate_name'],ENT_QUOTES,"UTF-8")):'';
            $Post['product_name'] = !empty($Post['product_name'])? trim(htmlspecialchars($Post['product_name'],ENT_QUOTES,"UTF-8")):'';
            $Post['brand_name'] = !empty($Post['brand_name'])? trim(htmlspecialchars($Post['brand_name'],ENT_QUOTES,"UTF-8")):'';
            $Post['price'] = !empty($Post['price'])? trim(htmlspecialchars($Post['price'],ENT_QUOTES,"UTF-8")):'';
            $Post['currency_id'] = !empty($Post['currency_id'])? trim(htmlspecialchars($Post['currency_id'],ENT_QUOTES,"UTF-8")):'';
            $Post['num'] = !empty($Post['num'])? trim(htmlspecialchars($Post['num'],ENT_QUOTES,"UTF-8")):'';
            $Post['service_id'] = !empty($Post['service_id'])? trim(htmlspecialchars($Post['service_id'],ENT_QUOTES,"UTF-8")):'';
            $Post['province_id'] = !empty($Post['province_id'])? trim(htmlspecialchars($Post['province_id'],ENT_QUOTES,"UTF-8")):'';
            $Post['city_id'] = !empty($Post['city_id'])? trim(htmlspecialchars($Post['city_id'],ENT_QUOTES,"UTF-8")):'';
            $Post['area_id'] = !empty($Post['area_id'])? trim(htmlspecialchars($Post['area_id'],ENT_QUOTES,"UTF-8")):'';
            $Post['delivery_area'] = !empty($Post['delivery_area'])? trim(htmlspecialchars($Post['delivery_area'],ENT_QUOTES,"UTF-8")):'';
            $Post['delivery_time'] = !empty($Post['delivery_time'])? trim(htmlspecialchars($Post['delivery_time'],ENT_QUOTES,"UTF-8")):'';
            $user = new UserModel();
            $error_array = array();
            //开始验证数据的完整性和有效性
            //公司
            $business = new UsersinvoiceModel();
            $business_info = $business->getUserBusinessyName("id= '{$Post['business_id']}' and user_id = '{$this->_logininfo['id']}'");
            if(empty($business_info)){
                $error_array = array(
                    'code'=>'1',
                    'msg'=>"请正确填写公司!"
                );
                echo json_encode($error_array);die();
            }

            //查询产品的id
            if(empty($Post['product_id'])){
                if(!empty($Post['product_name'])&&!empty($Post['brand_name'])&&!empty($Post['cate_name'])){
                    $product = new ProductModel();
                    //这边创建产品
                    $product_id = $product->getProdutByName($Post['product_name'],$Post['brand_name'],$Post['cate_name']);
                    if(!$product_id){
                        //这边插入产品,
                        $product_id = $product->addProdutByName($Post['product_name'],$Post['brand_name'],$Post['cate_name']);
                    }
                }
            }else{
                $product_id = $Post['product_id'];
            }


            if(!empty($Post['area_id'])){
                $area_id= $Post['area_id'];
            }else if(!empty($Post['city_id'])){
                $area_id= $Post['city_id'];
            }else{
                $area_id= $Post['province_id'];
            }
            //这边还要过滤掉重复的报价信息
            //查询当前用户的所有报价
            $quote = new QuotationModel();
            $quotelist = $quote->getDetailsById("iq_quotation.user_id = '{$this->_logininfo['id']}' and iq_quotation.business_id = '{$Post['business_id']}' and iq_quotation.product_id = '{$product_id}' and iq_quotation.service_id = '{$Post['service_id']}' and iq_quotation.currency_id = '{$Post['currency_id']}' and iq_quotation.delivery_time = '{$Post['delivery_time']}' and iq_quotation.area_id = '{$area_id}' and iq_quotation.status <> '4'");
//           var_dump($quotelist);die();
            if(!empty($quotelist)){
                //这边就是只更新数据
                $quote_data = array(
                    'update_time'=>date("Y-m-d H:i:s",strtotime('now')),
                    'update_uid'=>$this->_logininfo['id'],
                    'status'=>'1',
                    'price'=>$Post['price'],
                    'num'=>$Post['num'],
                    'remark'=>'单条报价，重复数据进行更新',
                );
                $res = $user -> saveeditquote($quote_data,$quotelist['id']);
                if($res && $res>0){
                    $error_array = array(
                        'code'=>'1008',
                        'msg'=>''
                    );
                    echo json_encode($error_array);die();
                }else{
                    $error_array = array(
                        'code'=>'1004',
                        'msg'=>'报价失败'
                    );
                    echo json_encode($error_array);die();
                }
            }

            $data = array(
                'create_uid'=>$this->_logininfo['id'],
                'create_time'=>$this->_nowtime,
                'update_time'=>$this->_nowtime,
                'update_uid'=>$this->_logininfo['id'],
                'organization_id'=>"1",
                'department_id'=>"1",
                'user_id'=>$this->_logininfo['id'],
                'business_id'=>$Post['business_id'],
                'type'=>'outer',
                'status'=>$_suppliyStateCfg['audit'],
                'product_id'=>$product_id,
                'service_id'=>$Post['service_id'],
                'currency_id'=>$Post['currency_id'],
                'price'=>$Post['price'],
                'settlement'=>empty($Post['settlement'])? '': $Post['settlement'],
                'num'=>$Post['num'],
                'delivery_area'=>$Post['delivery_area'],
                'delivery_time'=>$Post['delivery_time'],
                'delivery_method'=>empty($Post['delivery_method'])? '1': $Post['delivery_method'],
                'area_id'=>$area_id,

                'deposit_rate'=>"",
                'tray_type'=>"",
                'rebate_price'=>"0",
                'vice_uom_id'=>"",
                'vice_rate'=>"",
                'buy_qty'=>"0",
            );

            $res = $user->savequote($data);
            if($res && $res>0){
                $error_array = array(
                    'code'=>'1008',
                    'msg'=>''
                );
                echo json_encode($error_array);die();
            }else{
                $error_array = array(
                    'code'=>'1004',
                    'msg'=>'报价失败'
                );
                echo json_encode($error_array);die();
            }
        die();
        }
    }


    //报价管理
    public function quotemanageAction(){
        global $_suppliyStateMean,$_suppliyStateCfg,$_getAreaNames;
        define ( 'ITEM_PER_PAGE', 10 );
        $this->_title ="报价管理";
        $this->_keywords = '报价管理';
        $this->_description = '报价管理';
        $this->_view->title = $this->_title;
        $this->_view->keywords = $this->_keywords;
        $this->_view->description = $this->_description;
        $page=empty($_GET['page']) ? 1 :htmlspecialchars(trim($_GET['page']) , ENT_QUOTES ,'utf-8');
        $kd=empty($_GET['kd']) ? '' :htmlspecialchars(trim($_GET['kd']) , ENT_QUOTES ,'utf-8');
        $url = "/user/quotemanage";
        $index = new IndexModel();
        $cate = $index->getCateInfo("cate",'','');
        //这边获取公司的id---默认的
        $quote = new QuotationModel();

        $business = new UsersinvoiceModel();
        $business_info = $business->getUserInfoById($this->_logininfo['id'],'Y','r');

        $com = new CommonModel();
        $service_info = $com->getService_info("1=1");

        //报价单更新时间



        //导航标签数量
        $where_all = "iq_quotation.status in({$_suppliyStateCfg['audit']},{$_suppliyStateCfg['up']},{$_suppliyStateCfg['down']},{$_suppliyStateCfg['cancel']}) AND iq_quotation.user_id = {$this->_logininfo['id']}";
        $totalItems_all =  $quote->getQuoteCountByWhere($where_all);

        $where_audit = "iq_quotation.status in({$_suppliyStateCfg['audit']}) AND iq_quotation.user_id = {$this->_logininfo['id']}";
        $totalItems_audit =  $quote->getQuoteCountByWhere($where_audit);

        $where_up = "iq_quotation.status in({$_suppliyStateCfg['up']}) AND iq_quotation.user_id = {$this->_logininfo['id']}";
        $totalItems_up =  $quote->getQuoteCountByWhere($where_up);

        $where_down = "iq_quotation.status in({$_suppliyStateCfg['down']}) AND iq_quotation.user_id = {$this->_logininfo['id']}";
        $totalItems_down =  $quote->getQuoteCountByWhere($where_down);

        $where_cancel = "iq_quotation.status in({$_suppliyStateCfg['cancel']}) AND iq_quotation.user_id = {$this->_logininfo['id']}";
        $totalItems_cancel =  $quote->getQuoteCountByWhere($where_cancel);

        //获取查询参数
        $cate_name=empty($_GET['cate_name']) ? '' :htmlspecialchars(trim($_GET['cate_name']) , ENT_QUOTES ,'utf-8');

        $brand_name=empty($_GET['brand_name']) ? '' :htmlspecialchars(trim($_GET['brand_name']) , ENT_QUOTES ,'utf-8');

        $product_name=empty($_GET['product_name']) ? '' :htmlspecialchars(trim($_GET['product_name']) , ENT_QUOTES ,'utf-8');

        $status=empty($_GET['status']) ? '' :htmlspecialchars(trim($_GET['status']) , ENT_QUOTES ,'utf-8');

        $condition = array();
        $urls=array();
        if(!empty($cate_name)){
            $condition[] = "product_cates.cate_name like '%{$cate_name}%'";
            $urls[]="cate_name={$cate_name}";
        }

        if(!empty($product_name)){
            $condition[] = "product.product_name like '%{$product_name}%'";
            $urls[]="product_name={$product_name}";
        }

        if(!empty($brand_name)){
            $condition[] = "product_brands.brand_name like '%{$brand_name}%'";
            $urls[]="brand_name={$brand_name}";
        }

        if(!empty($status)){
            $condition[] = "iq_quotation.status = '{$status}'";
            $urls[]="status={$status}";
        }
        $where = "1=1";
        if(empty($condition))
        {
            $where  .= " AND iq_quotation.user_id = {$this->_logininfo['id']} and iq_quotation.type not in ('require')";
        } else {
            $where  .=" AND iq_quotation.user_id = {$this->_logininfo['id']} and iq_quotation.type not in ('require')". "  AND ".implode(' AND ', $condition);
        }
        $url .= "?page=%d";
        if(!empty($urls)){
            $url.=  "&".implode('&',$urls);
        }

        $order = "iq_quotation.create_time desc";
        $group = "";

        //这边开始
        $totalItems =  $quote->getQuoteCountByWhere($where);
        $offset = 0;
        $page =  empty($page) ? 1 :(int)$page;
        $offset = ($page - 1) * ITEM_PER_PAGE;
        if ($offset >= $totalItems) {
            $page = ceil ( $totalItems / ITEM_PER_PAGE );
            $offset = ($page - 1) * ITEM_PER_PAGE;
        }
        if ($offset < 0) {
            $offset = 0;
            $page = 1;
        }

        $data = $quote->getQuoteListByWhere($where,ITEM_PER_PAGE,$offset,$order,$group);
//        var_dump($data);
        $links = Pagination::createPage($url, ITEM_PER_PAGE, $page, $totalItems, 2 ,'_self',true);
        $this->_view->cate = $cate;
        $this->_view->kd = $kd;
        $this->_view->business_info = $business_info;
        $this->_view->service_info = $service_info;
        $this->_view->links = $links;
        $this->_view->data = $data;
        $this->_view->_suppliyStateMean = $_suppliyStateMean;
        $this->_view->totalItems_all = $totalItems_all;
        $this->_view->totalItems_audit = $totalItems_audit;
        $this->_view->totalItems_up = $totalItems_up;
        $this->_view->totalItems_down = $totalItems_down;
        $this->_view->totalItems_cancel = $totalItems_cancel;

        $this->_view->cate_name = $cate_name;
        $this->_view->brand_name = $brand_name;
        $this->_view->product_name = $product_name;
        $this->_view->status = $status;
        $this->_view->_getAreaNames = $_getAreaNames;
    }
    //这边修改报价单管理
    public function saveeditquoteAction(){
        global $_suppliyStateCfg,$_suppliyStateMean,$_settlement,$_orderDeliveryTypeMean,$_orderDeliveryTypeCfg;
        if($this->_request->isPost()) {
            $Post = $this->_request->getPost();
            $Post['quote_id'] = !empty($Post['quote_id']) ? trim(htmlspecialchars($Post['quote_id'], ENT_QUOTES, "UTF-8")) : '';
            $Post['product_id'] = !empty($Post['product_id']) ? trim(htmlspecialchars($Post['product_id'], ENT_QUOTES, "UTF-8")) : '';
            $Post['cate_name'] = !empty($Post['cate_name']) ? trim(htmlspecialchars($Post['cate_name'], ENT_QUOTES, "UTF-8")) : '';
            $Post['product_name'] = !empty($Post['product_name']) ? trim(htmlspecialchars($Post['product_name'], ENT_QUOTES, "UTF-8")) : '';
            $Post['brand_name'] = !empty($Post['brand_name']) ? trim(htmlspecialchars($Post['brand_name'], ENT_QUOTES, "UTF-8")) : '';
            $Post['price'] = !empty($Post['price']) ? trim(htmlspecialchars($Post['price'], ENT_QUOTES, "UTF-8")) : '';
            $Post['num'] = !empty($Post['num']) ? trim(htmlspecialchars($Post['num'], ENT_QUOTES, "UTF-8")) : '';
            $Post['service_id'] = !empty($Post['service_id']) ? trim(htmlspecialchars($Post['service_id'], ENT_QUOTES, "UTF-8")) : '';
            $Post['area_name'] = !empty($Post['area_name']) ? trim(htmlspecialchars($Post['area_name'], ENT_QUOTES, "UTF-8")) : '';
            $Post['delivery_time'] = !empty($Post['delivery_time']) ? trim(htmlspecialchars($Post['delivery_time'], ENT_QUOTES, "UTF-8")) : '';


            //查询产品的id
            if(!empty($Post['product_name'])&&!empty($Post['brand_name'])&&!empty($Post['cate_name'])){
                $product = new ProductModel();
                //这边创建产品
                $product_id = $product->getProdutByName($Post['product_name'],$Post['brand_name'],$Post['cate_name']);
                if(!$product_id){
                    //这边插入产品,
                    $product_id = $product->addProdutByName($Post['product_name'],$Post['brand_name'],$Post['cate_name']);
                }
            }

            //这边需要判断产品是否改变
            if($Post['product_id']==$product_id){
                $product_id = $Post['product_id'];
            }
            $com = new CommonModel();
            $area_info = $com->getArea("region_name like '%{$Post['area_name']}%'");
            $data = array(
                'update_time'=>$this->_nowtime,
                'update_uid'=>$this->_logininfo['id'],
                'status'=>$_suppliyStateCfg['audit'],
                'product_id'=>$product_id,
                'service_id'=>$Post['service_id'],
                'price'=>$Post['price'],
                'num'=>$Post['num'],
                'area_id'=>empty($area_info) ? 0 :$area_info['region_id'],
                'delivery_time'=>$Post['delivery_time'],
            );
            $user = new UserModel();
            $res = $user->saveeditquote($data,$Post['quote_id']);
            if($res && $res>0){
                $tip = array(
                    'code'=>'1008',
                    'msg'=>''
                );
                echo json_encode($tip);die();
            }else{
                $tip = array(
                    'code'=>'1004',
                    'msg'=>'报价失败'
                );
                echo json_encode($tip);die();
            }
            die();
        }
    }
    //删除报价单
    public function delquoteAction(){
        if($this->_request->isPost()) {
            $Post = $this->_request->getPost();
            $Post['id']  = empty($Post['id'])? '': (int)$Post['id'];
            $quote = new QuotationModel();

            if(empty($Post['id'])){
                die("1");
            }
            //这边提示：已经下单的报价单，咱无法删除

            $sale = new SaleorderModel();

            $sale_order = $sale->getOrderByQuoteId($Post['id']);

            if(!empty($sale_order)){
                die("3");
            }

           $res =  $quote->delquote($Post['id']);

            if($res && $res>0){
                die("5");
            }else{
                die("2");
            }
        die();
        }
    }
    //上架报价
    public function quoteupAction(){
        global $_suppliyStateCfg;
        if($this->_request->isPost()) {
            $Post = $this->_request->getPost();
            $Post['check']  = empty($Post['check'])? '': $Post['check'];//这已经是数组
            $user = new UserModel();
            $quote = new QuotationModel();
            if(empty($Post['check'])){
                die("1");
            }
//            var_dump($Post['check']);
            //这边提示：待审核和审核不用过的不许上架
            foreach($Post['check'] as $key=>$var){
                $status = $quote->getQuoteStatusByWhere("id = '{$var}'");
                if($status=='1'){//这是待审核
                    die("2");
                }
                if($status=='4'){//这是审核不通过
                    die("3");
                }
                $data = array(
                    'update_time'=>$this->_nowtime,
                    'update_uid'=>$this->_logininfo['id'],
                    'status'=>$_suppliyStateCfg['up'],
                );

                $res = $user->saveeditquote($data,$var);
                if($res && $res>0){
                    die("5");
                }else{
                    die("4");
                }
            }

            die();
        }
    }
    //下架报价
    public function quotedownAction(){
        global $_suppliyStateCfg;
        if($this->_request->isPost()) {
            $Post = $this->_request->getPost();
            $Post['check']  = empty($Post['check'])? '': $Post['check'];//这已经是数组
            $user = new UserModel();
            $quote = new QuotationModel();
            if(empty($Post['check'])){
                die("1");
            }
//            var_dump($Post['check']);
            //这边提示：待审核和审核不用过的不许上架
            foreach($Post['check'] as $key=>$var){
                $status = $quote->getQuoteStatusByWhere("id = '{$var}'");
                if($status=='1'){//这是待审核
                    die("2");
                }
                if($status=='4'){//这是审核不通过
                    die("3");
                }
                $data = array(
                    'update_time'=>$this->_nowtime,
                    'update_uid'=>$this->_logininfo['id'],
                    'status'=>$_suppliyStateCfg['down'],
                );

                $res = $user->saveeditquote($data,$var);
                if($res && $res>0){
                    die("5");
                }else{
                    die("4");
                }
            }

            die();
        }
    }
    //批量删除
    public function quotedelAction(){
        global $_suppliyStateCfg;
        if($this->_request->isPost()) {
            $Post = $this->_request->getPost();
            $Post['check']  = empty($Post['check'])? '': $Post['check'];//这已经是数组
            $user = new UserModel();
            $sale = new SaleorderModel();
            $quote = new QuotationModel();
            if(empty($Post['check'])){
                die("1");
            }
//            var_dump(implode(",",$Post['check']));die();
            //这边提示：下过订单的报价不能删除
            foreach($Post['check'] as $key=>$var){

                $sale_order = $sale->getOrderByQuoteId($var);
                if(!empty($sale_order)){
                    die("2");
                }
            }

            $res = $quote->delquote(implode(",",$Post['check']));

            if($res && $res>0){
                die("4");
            }else{
                die("3");
            }

            die();
        }
    }
    //下载
    public function quotedownloadAction(){
        global $_suppliyStateCfg;
        if($this->_request->isPost()) {
            $Post = $this->_request->getPost();
            $Post['check']  = empty($Post['check'])? '': $Post['check'];//这已经是数组
            $user = new UserModel();
            $quote = new QuotationModel();
            if(empty($Post['check'])){
                die("1");
            }
            die("8");
        }
    }
    public function downlistAction(){
        ob_clean();
        $check = empty($_GET['check']) ? '' :htmlspecialchars(trim($_GET['check']) , ENT_QUOTES ,'utf-8');
        $quote = new QuotationModel();
        $where = "iq_quotation.id in($check)";
        $feild=array(
            'id',
            'business_name',
            "cate_name",
            "product_name",
            "brand_name",
            "service_name",
            "price",
            "currency_id",
            "settlement",
            "num",
            "delivery_area",
            "delivery_time",
        );

        $quote_list = $quote->getDownList($where);
//            var_dump($quote_list);die();
        $excelHeadData = array (
            "business_name" => "供应商名称",
            "cate_name" => "品种",
            "product_name" => "牌号",
            "brand_name" => "厂商",
            "service_name" => "交易类型",
            "price" => "价格(元/吨)",
            "currency_name" => "币种",
            "settlement" => "结算方式",
            "num" => "数量(吨)",
            "delivery_method" => "提货方式",
            "delivery_area" => "交货地点",
            "delivery_time" => "交货时间",
        );

        $phpExcel = new PHPExcel ();
        // 读取模版
        $saleFile = "excelModel/downquote.xls";
        $PHPReader = new PHPExcel_Reader_Excel5 ();
        if ($PHPReader->canRead ( $saleFile )) {
            $phpExcel = $PHPReader->load ( $saleFile );
        }
        // 定义输出的表名
        $filename = "供应商报价单" . date ( "YmdHis" ) . ".xls";
        // 设置字体颜色为红色
        $styleFontColorRed = array (
            "font" => array (
                "color" => array (
                    "argb" => "FFFF0000"
                )
            )
        );
        // 设置表头
        $key = ord ( "A" );
        foreach ( $excelHeadData as $key1 => $value ) {
            $colum = chr ( $key );
            $phpExcel->setActiveSheetIndex ( 0 )->setCellValue ( $colum . '1', $value );
            $key += 1;
        }
        // 循环读入数据的内容
        $column = 2;
        foreach ( $quote_list as $key1 => $val) {
            // 格式化存入的内容
            if(!empty($val['delivery_method'])){
                if($val['delivery_method']=='2'){
                    $val['delivery_method'] = "配送";
                }else{
                    $val['delivery_method'] = "自提";
                }
            }else{
                $val['delivery_method'] = "自提";
            }
            $excelData = array (
                "business_name" => $val['business_name'],
                "cate_name" => $val['cate_name'],
                "product_name" => $val['product_name'],
                "brand_name" => $val['brand_name'],
                "service_name" => $val['service_name'],
                "price" => $val['price'],
                "currency_name" => $val['currency_name'],
                "settlement" => $val['settlement'],
                "num" => $val['num'],
                "delivery_method" => $val['delivery_method'],
                "delivery_area" => $val['region_name'],
                "delivery_time" => $val['delivery_time'],
            ) ;
            // 设置内容数据
            $key = ord ( "A" );
            foreach ( $excelData as $key2 => $value2 ) {
                $colum = chr ( $key );
                //C行全部设置为文本型的数据
                if($colum =="C"){
                    $phpExcel->getActiveSheet()->setCellValueExplicit('C'.$column,$value2,PHPExcel_Cell_DataType::TYPE_STRING);
                }else{
                    $phpExcel->setActiveSheetIndex ( 0 )->setCellValue ( $colum . $column, $value2 );
                }
                // 状态为取消的时候设置为红色
//                if ("已取消" === $value2) {
//                    $phpExcel->getActiveSheet ()->getStyle ( $colum . $column )->applyFromArray ( $styleFontColorRed );
//                }
                $key += 1;
            }
            $column ++;
        }

        // 设置筛选 筛选一定是一个范围 如果是每列筛选 就筛选全表 从A1 到最后一列一行的结尾
        $phpExcel->getActiveSheet ()->setAutoFilter ( 'A1:T' . $column );
        $filename = iconv ( "utf-8", "gb2312", $filename );
        $obj_Writer = PHPExcel_IOFactory::createWriter ( $phpExcel, 'Excel5' );

        header ( "Content-Type: application/force-download" );
        header ( "Content-Type: application/octet-stream" );
        header ( "Content-Type: application/download" );
        header ( 'Content-Disposition:inline;filename="' . $filename . '"' );
        header ( "Content-Transfer-Encoding: binary" );
        header ( "Last-Modified: " . gmdate ( "D, d M Y H:i:s" ) . " GMT" );
        header ( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
        header ( "Pragma: no-cache" );
        $obj_Writer->save ( 'php://output' );
        exit ();
    }
    //刷新报价
    public function updatequoteAction(){
        global $_suppliyStateCfg;
        //这边是刷新报价的
        if($this->_request->isPost()) {
            $Post = $this->_request->getPost();
            $Post['bid'] = empty($Post['bid']) ? '' : $Post['bid'];//这已经是数组
            if(empty($Post['bid'])){
                die("100");
            }
            $user = new UserModel();
            $data = array(
                'update_time' => $this->_nowtime
            );
            //这边考虑是否有报价
            $quote_info = $user->getquoteByuserId($this->_logininfo['id']);
            if (empty($quote_info)) {
                die("1");
            }

            $res = $user->setquoteupdate($data, "user_id = '{$this->_logininfo['id']}'");
            if ($res && $res > 0) {
                //这边唯一添加的东西：更新报价时间  quotation_update_time
                $business = new UsersinvoiceModel();
                $business_data = array(
                    "quotation_update_time"=>$this->_nowtime
                );
                $business->updateBusinessByWhere($business_data,"id = '{$Post['bid']}'");

                die("2");
            } else {
                die("3");
            }
            die();
        }
    }


    //发布求购==========
    public function publishinquireAction(){
        global $_BuyStartMean,$_BuyStateCfg,$_getAreaNames,$_getAreaname;
        $this->_title ="发布求购";
        $this->_keywords = '发布求购';
        $this->_description = '发布求购';
        $this->_view->title = $this->_title;
        $this->_view->keywords = $this->_keywords;
        $this->_view->description = $this->_description;

        $kd=empty($_GET['kd']) ? '' :htmlspecialchars(trim($_GET['kd']) , ENT_QUOTES ,'utf-8');

        $index = new IndexModel();
        $cate = $index->getCateInfo("cate",'','');

        //这边查询报价的公司
        $business = new UsersinvoiceModel();
        $business_info = $business->getUserInfoById($this->_logininfo['id'],'','a');

        $com = new CommonModel();

        $service_info = $com->getService_info("1=1");


        $this->_view->cate = $cate;
        $this->_view->kd = $kd;


        $this->_view->business_info = $business_info;
        $this->_view->service_info = $service_info;
        $this->_view->_getAreaNames = $_getAreaNames;
        $this->_view->_getAreaname = $_getAreaname;

    }
    //保存提交的求购
    public function savepublishinquireAction(){
        global $_BuyStartMean,$_BuyStateCfg;
        if($this->_request->isPost()) {
            $Post = $this->_request->getPost();
            $Post['product_id'] = !empty($Post['product_id'])? trim(htmlspecialchars($Post['product_id'],ENT_QUOTES,"UTF-8")):'';
            $Post['business_id'] = !empty($Post['business_id'])? trim(htmlspecialchars($Post['business_id'],ENT_QUOTES,"UTF-8")):'';
            $Post['cate_name'] = !empty($Post['cate_name'])? trim(htmlspecialchars($Post['cate_name'],ENT_QUOTES,"UTF-8")):'';
            $Post['product_name'] = !empty($Post['product_name'])? trim(htmlspecialchars($Post['product_name'],ENT_QUOTES,"UTF-8")):'';
            $Post['brand_name'] = !empty($Post['brand_name'])? trim(htmlspecialchars($Post['brand_name'],ENT_QUOTES,"UTF-8")):'';
            $Post['price'] = !empty($Post['price'])? trim(htmlspecialchars($Post['price'],ENT_QUOTES,"UTF-8")):'';
            $Post['currency_id'] = !empty($Post['currency_id'])? trim(htmlspecialchars($Post['currency_id'],ENT_QUOTES,"UTF-8")):'';
            $Post['num'] = !empty($Post['num'])? trim(htmlspecialchars($Post['num'],ENT_QUOTES,"UTF-8")):'';
            $Post['service_id'] = !empty($Post['service_id'])? trim(htmlspecialchars($Post['service_id'],ENT_QUOTES,"UTF-8")):'';
            $Post['province_id'] = !empty($Post['province_id'])? trim(htmlspecialchars($Post['province_id'],ENT_QUOTES,"UTF-8")):'';
            $Post['city_id'] = !empty($Post['city_id'])? trim(htmlspecialchars($Post['city_id'],ENT_QUOTES,"UTF-8")):'';
            $Post['area_id'] = !empty($Post['area_id'])? trim(htmlspecialchars($Post['area_id'],ENT_QUOTES,"UTF-8")):'';
            $Post['delivery_area'] = !empty($Post['delivery_area'])? trim(htmlspecialchars($Post['delivery_area'],ENT_QUOTES,"UTF-8")):'';
            $Post['delivery_time'] = !empty($Post['delivery_time'])? trim(htmlspecialchars($Post['delivery_time'],ENT_QUOTES,"UTF-8")):'';
            $user = new UserModel();
            $error_array = array();

            //开始验证数据的完整性和有效性
            //公司
            $business = new UsersinvoiceModel();
            $business_info = $business->getUserBusinessyName("id= '{$Post['business_id']}' and user_id = '{$this->_logininfo['id']}'");
            if(empty($business_info)){
                $error_array = array(
                    'code'=>'1',
                    'msg'=>"请正确填写公司!"
                );
                echo json_encode($error_array);die();
            }

            //查询产品的id
            if(empty($Post['product_id'])){
                if(!empty($Post['product_name'])&&!empty($Post['brand_name'])&&!empty($Post['cate_name'])){
                    $product = new ProductModel();
                    //这边创建产品
                    $product_id = $product->getProdutByName($Post['product_name'],$Post['brand_name'],$Post['cate_name']);
                    if(!$product_id){
                        //这边插入产品,
                        $product_id = $product->addProdutByName($Post['product_name'],$Post['brand_name'],$Post['cate_name']);
                    }
                }
            }else{
                $product_id = $Post['product_id'];
            }


            if(!empty($Post['area_id'])){
                $area_id= $Post['area_id'];
            }else if(!empty($Post['city_id'])){
                $area_id= $Post['city_id'];
            }else{
                $area_id= $Post['province_id'];
            }
            //这边要求过滤重复求购数据，防止恶意刷数据
            //查询同公司，产品，类型，货币，交货地点，地区，时间
            $inquire = new InquireModel();
            $inquirelist = $inquire->getInquireList("iq.user_id = '{$this->_logininfo['id']}' and iq.business_id = '{$Post['business_id']}' and iq.product_id = '{$product_id}' and iq.service_id = '{$Post['service_id']}' and iq.currency_id = '{$Post['currency_id']}' and iq.delivery_time = '{$Post['delivery_time']}' and iq.area_id = '{$area_id}' and iq.status in ('1','2')");

            if(!empty($inquirelist)){
                //这边更新求购数据
                $inquire_data = array(
                    'update_time'=>$this->_nowtime,
                    'update_uid'=>$this->_logininfo['id'],
                    'status'=>$_BuyStateCfg['wait_audit'],
                    'price'=>$Post['price'],
                    'num'=>$Post['num'],
                    'remark'=>'发布求购时，更新重复的求购数据'
                );

                $res =  $user->saveeditinquire($inquire_data,$inquirelist['id']);
                if($res && $res>0){
                    $error_array = array(
                        'code'=>'1008',
                        'msg'=>''
                    );
                    echo json_encode($error_array);die();
                }else{
                    $error_array = array(
                        'code'=>'1004',
                        'msg'=>'求购失败'
                    );
                    echo json_encode($error_array);die();
                }
            }

            $data = array(
                'create_uid'=>$this->_logininfo['id'],
                'create_time'=>$this->_nowtime,
                'update_time'=>$this->_nowtime,
                'update_uid'=>$this->_logininfo['id'],
                'user_id'=>$this->_logininfo['id'],
                'business_id'=>$Post['business_id'],
                'type'=>'require',
                'status'=>$_BuyStateCfg['wait_audit'],
                'product_id'=>$product_id,
                'service_id'=>$Post['service_id'],
                'currency_id'=>$Post['currency_id'],
                'price'=>$Post['price'],
                'num'=>$Post['num'],
                'delivery_area'=>$Post['delivery_area'],
                'delivery_time'=>$Post['delivery_time'],
                'delivery_method'=>empty($Post['delivery_method'])? '1': $Post['delivery_method'],
                'area_id'=>$area_id,
                'vice_uom_id'=>"",
                'vice_rate'=>"",
            );


            $res = $user->saveinquire($data);
            if($res && $res>0){
                $error_array = array(
                    'code'=>'1008',
                    'msg'=>''
                );
                echo json_encode($error_array);die();
            }else{
                $error_array = array(
                    'code'=>'1004',
                    'msg'=>'求购失败'
                );
                echo json_encode($error_array);die();
            }
            die();
        }
    }

    //求购管理
    public function inquiremanageAction(){
        global $_BuyStartMean,$_BuyStateCfg,$_getAreaNames;
        define ( 'ITEM_PER_PAGE', 10 );
        $this->_title ="求购管理";
        $this->_keywords = '求购管理';
        $this->_description = '求购管理';
        $this->_view->title = $this->_title;
        $this->_view->keywords = $this->_keywords;
        $this->_view->description = $this->_description;
        $page=empty($_GET['page']) ? 1 :htmlspecialchars(trim($_GET['page']) , ENT_QUOTES ,'utf-8');
        $kd=empty($_GET['kd']) ? '' :htmlspecialchars(trim($_GET['kd']) , ENT_QUOTES ,'utf-8');
        $url = "/user/inquiremanage";
        $index = new IndexModel();
        $cate = $index->getCateInfo("cate",'','');
        $inquire = new InquireModel();
        //导航标签数量
        $where_all = "iq_inquiry.status in({$_BuyStateCfg['wait_audit']},{$_BuyStateCfg['audit']},{$_BuyStateCfg['Negotiate']},{$_BuyStateCfg['Trading']},{$_BuyStateCfg['done']},{$_BuyStateCfg['cancel']}) AND iq_inquiry.user_id = {$this->_logininfo['id']}";
        $totalItems_all =  $inquire->getInquireCountByWhere($where_all);

        $where_wait_audit = "iq_inquiry.status in({$_BuyStateCfg['wait_audit']}) AND iq_inquiry.user_id = {$this->_logininfo['id']}";
        $totalItems_wait_audit =  $inquire->getInquireCountByWhere($where_wait_audit);

        $where_audit = "iq_inquiry.status in({$_BuyStateCfg['audit']}) AND iq_inquiry.user_id = {$this->_logininfo['id']}";
        $totalItems_audit =  $inquire->getInquireCountByWhere($where_audit);

        $where_Negotiate = "iq_inquiry.status in({$_BuyStateCfg['Negotiate']}) AND iq_inquiry.user_id = {$this->_logininfo['id']}";
        $totalItems_Negotiate =  $inquire->getInquireCountByWhere($where_Negotiate);

        $where_Trading = "iq_inquiry.status in({$_BuyStateCfg['Trading']}) AND iq_inquiry.user_id = {$this->_logininfo['id']}";
        $totalItems_Trading =  $inquire->getInquireCountByWhere($where_Trading);

        $where_done = "iq_inquiry.status in({$_BuyStateCfg['done']}) AND iq_inquiry.user_id = {$this->_logininfo['id']}";
        $totalItems_done =  $inquire->getInquireCountByWhere($where_done);

        $where_cancel = "iq_inquiry.status in({$_BuyStateCfg['cancel']}) AND iq_inquiry.user_id = {$this->_logininfo['id']}";
        $totalItems_cancel =  $inquire->getInquireCountByWhere($where_cancel);

        //获取查询参数
        $cate_name=empty($_GET['cate_name']) ? '' :htmlspecialchars(trim($_GET['cate_name']) , ENT_QUOTES ,'utf-8');

        $brand_name=empty($_GET['brand_name']) ? '' :htmlspecialchars(trim($_GET['brand_name']) , ENT_QUOTES ,'utf-8');

        $product_name=empty($_GET['product_name']) ? '' :htmlspecialchars(trim($_GET['product_name']) , ENT_QUOTES ,'utf-8');

        $delivery_area=empty($_GET['delivery_area']) ? '' :htmlspecialchars(trim($_GET['delivery_area']) , ENT_QUOTES ,'utf-8');

        $status=empty($_GET['status']) ? '' :htmlspecialchars(trim($_GET['status']) , ENT_QUOTES ,'utf-8');

        $condition = array();
        $urls=array();
        if(!empty($cate_name)){
            $condition[] = "product_cates.cate_name like '%{$cate_name}%'";
            $urls[]="cate_name={$cate_name}";
        }

        if(!empty($product_name)){
            $condition[] = "product.product_name like '%{$product_name}%'";
            $urls[]="product_name={$product_name}";
        }

        if(!empty($brand_name)){
            $condition[] = "product_brands.brand_name like '%{$brand_name}%'";
            $urls[]="brand_name={$brand_name}";
        }

        if(!empty($delivery_area)){
            $condition[] = "iq_inquiry.delivery_area like '%{$delivery_area}%'";
            $urls[]="delivery_area={$delivery_area}";
        }

        if(!empty($status)){
            $condition[] = "iq_inquiry.status = '{$status}'";
            $urls[]="status={$status}";
        }
        $where = "1=1";
        if(empty($condition))
        {
            $where  .= " AND iq_inquiry.user_id = {$this->_logininfo['id']}";
        } else {
            $where  .=" AND iq_inquiry.user_id = {$this->_logininfo['id']}". "  AND ".implode(' AND ', $condition);
        }
        $url .= "?page=%d";
        if(!empty($urls)){
            $url.=  "&".implode('&',$urls);
        }

        $order = "iq_inquiry.create_time desc";
        $group = "";

        //这边开始
        $totalItems =  $inquire->getInquireCountByWhere($where);
        $offset = 0;
        $page =  empty($page) ? 1 :(int)$page;
        $offset = ($page - 1) * ITEM_PER_PAGE;
        if ($offset >= $totalItems) {
            $page = ceil ( $totalItems / ITEM_PER_PAGE );
            $offset = ($page - 1) * ITEM_PER_PAGE;
        }
        if ($offset < 0) {
            $offset = 0;
            $page = 1;
        }

        $data = $inquire->getInquireListByWhere($where,ITEM_PER_PAGE,$offset,$order,$group);
//        var_dump($data);
        $links = Pagination::createPage($url, ITEM_PER_PAGE, $page, $totalItems, 2 ,'_self',true);


        $this->_view->cate = $cate;
        $this->_view->kd = $kd;
        $this->_view->data = $data;
        $this->_view->links = $links;
        $this->_view->_BuyStartMean = $_BuyStartMean;

        $this->_view->cate_name = $cate_name;
        $this->_view->brand_name = $brand_name;
        $this->_view->product_name = $product_name;
        $this->_view->delivery_area = $delivery_area;
        $this->_view->status = $status;

        $this->_view->totalItems_all = $totalItems_all;
        $this->_view->totalItems_wait_audit = $totalItems_wait_audit;
        $this->_view->totalItems_audit = $totalItems_audit;
        $this->_view->totalItems_Negotiate = $totalItems_Negotiate;
        $this->_view->totalItems_Trading = $totalItems_Trading;
        $this->_view->totalItems_done = $totalItems_done;
        $this->_view->totalItems_cancel = $totalItems_cancel;
        $this->_view->_getAreaNames = $_getAreaNames;
    }
    //删除求购单
    public function delinquireAction(){
        if($this->_request->isPost()) {
            $Post = $this->_request->getPost();
            $Post['id']  = empty($Post['id'])? '': (int)$Post['id'];
            $quote = new QuotationModel();
            $inquire = new InquireModel();

            if(empty($Post['id'])){
                die("1");
            }
            //这边提示：已经下单的报价单，咱无法删除
            $quote_order = $quote->getQuoteByInquireId("inquiry_id = '{$Post['id']}'");
            if(!empty($quote_order)){
                die("3");
            }

            $res =  $inquire->delinquire($Post['id']);

            if($res && $res>0){
                die("5");
            }else{
                die("2");
            }
            die();
        }
    }

    //批量删除求购
    public function inquiredelAction(){
        global $_suppliyStateCfg;
        if($this->_request->isPost()) {
            $Post = $this->_request->getPost();
            $Post['check']  = empty($Post['check'])? '': $Post['check'];//这已经是数组
            $inquire = new InquireModel();
            $quote = new QuotationModel();
            if(empty($Post['check'])){
                die("1");
            }
//            var_dump(implode(",",$Post['check']));die();
            //这边提示：报过价的求购单不能删除
            foreach($Post['check'] as $key=>$var){
                $quote_order = $quote->getQuoteByInquireId("inquiry_id = '{$var}'");
                if(!empty($quote_order)){
                    die("2");
                }
            }

            $res = $inquire->delinquire(implode(",",$Post['check']));

            if($res && $res>0){
                die("4");
            }else{
                die("3");
            }

            die();
        }
    }

    //我收到的报价
    public function inquirequotemanageAction(){
        global $_suppliyStateMean,$_suppliyStateCfg,$_getAreaNames;
        define ( 'ITEM_PER_PAGE', 5 );
        $this->_title ="我收到的报价";
        $this->_keywords = '我收到的报价';
        $this->_description = '我收到的报价';
        $this->_view->title = $this->_title;
        $this->_view->keywords = $this->_keywords;
        $this->_view->description = $this->_description;
        $url= "/user/inquirequotemanage";
        $kd=empty($_GET['kd']) ? '' :htmlspecialchars(trim($_GET['kd']) , ENT_QUOTES ,'utf-8');
        $page=empty($_GET['page']) ? '' :htmlspecialchars(trim($_GET['page']) , ENT_QUOTES ,'utf-8');
        $index = new IndexModel();
        $cate = $index->getCateInfo("cate",'','');
        $quote = new QuotationModel();
        //获取查询参数
        $cate_name=empty($_GET['cate_name']) ? '' :htmlspecialchars(trim($_GET['cate_name']) , ENT_QUOTES ,'utf-8');

        $brand_name=empty($_GET['brand_name']) ? '' :htmlspecialchars(trim($_GET['brand_name']) , ENT_QUOTES ,'utf-8');

        $product_name=empty($_GET['product_name']) ? '' :htmlspecialchars(trim($_GET['product_name']) , ENT_QUOTES ,'utf-8');

        $delivery_area=empty($_GET['delivery_area']) ? '' :htmlspecialchars(trim($_GET['delivery_area']) , ENT_QUOTES ,'utf-8');

        $status=empty($_GET['status']) ? '' :htmlspecialchars(trim($_GET['status']) , ENT_QUOTES ,'utf-8');

        $condition = array();
        $urls=array();
        if(!empty($cate_name)){
            $condition[] = "product_cates.cate_name like '%{$cate_name}%'";
            $urls[]="cate_name={$cate_name}";
        }

        if(!empty($product_name)){
            $condition[] = "product.product_name like '%{$product_name}%'";
            $urls[]="product_name={$product_name}";
        }

        if(!empty($brand_name)){
            $condition[] = "product_brands.brand_name like '%{$brand_name}%'";
            $urls[]="brand_name={$brand_name}";
        }

        if(!empty($delivery_area)){
            $condition[] = "iq_quotation.delivery_area like '%{$delivery_area}%'";
            $urls[]="delivery_area={$delivery_area}";
        }

        $where = "1=1";
        if(empty($condition))
        {
            $where  .= " AND iq_inquiry.user_id = '{$this->_logininfo['id']}'  AND iq_quotation.type = 'require'";
        } else {
            $where  .=" AND iq_inquiry.user_id = '{$this->_logininfo['id']}'  AND iq_quotation.type = 'require'". "  AND ".implode(' AND ', $condition);
        }
        $url .= "?page=%d";
        if(!empty($urls)){
            $url.=  "&".implode('&',$urls);
        }

        $order = "iq_quotation.id desc";
        $group = "";

        //这边开始
        $totalItems =  $quote->getInquireQuoteCountByWhere($where);
        $offset = 0;
        $page =  empty($page) ? 1 :$page;
        $offset = ($page - 1) * ITEM_PER_PAGE;
        if ($offset >= $totalItems) {
            $page = ceil ( $totalItems / ITEM_PER_PAGE );
            $offset = ($page - 1) * ITEM_PER_PAGE;
        }
        if ($offset < 0) {
            $offset = 0;
            $page = 1;
        }

        $data = $quote->getInquireQuoteListByWhere($where,ITEM_PER_PAGE,$offset,$order,$group);

        $links = Pagination::createPage($url, ITEM_PER_PAGE, $page, $totalItems, 2 ,'_self',true);


        $this->_view->cate = $cate;
        $this->_view->kd = $kd;
        $this->_view->data = $data;
        $this->_view->links = $links;
        $this->_view->totalItems = $totalItems;

        $this->_view->cate_name = $cate_name;
        $this->_view->brand_name = $brand_name;
        $this->_view->product_name = $product_name;
        $this->_view->delivery_area = $delivery_area;
        $this->_view->_getAreaNames = $_getAreaNames;
    }

    //取消报价下单
    public function setQuoteStatusAction(){
        global $_suppliyStateCfg;
        $id=empty($_GET['id']) ? '' :(int)htmlspecialchars(trim($_GET['id']) , ENT_QUOTES ,'utf-8');
        if(empty($id)){
            $this->redirect ( '/error/error404?error=报价信息获取失败' );
            die();
        }
        $quote = new QuotationModel();
        //这边检查id是否存在
        if($quote->getQuoteByInquireId("id = '{$id}' and type='require' and status='{$_suppliyStateCfg['up']}'")){
            //这边修改状态
            $data = array(
                "update_time"=>$this->_nowtime,
                "update_uid"=>$this->_logininfo['id'],
                "status"=>$_suppliyStateCfg['cancel'],
                "remark"=>"我收到的报价，求购商自己取消报价单",
            );
            $quote->setQuoteStatusByWhere("id = '{$id}'",$data);
            $this->redirect ( '/user/inquirequotemanage' );
            die();
        }else{
            $this->redirect ( '/error/error404?error=报价信息不存在' );
            die();
        }
        die();
    }

    public function setQuotesStatusAction(){
        global $_suppliyStateCfg;
        $id=empty($_GET['id']) ? '' :(int)htmlspecialchars(trim($_GET['id']) , ENT_QUOTES ,'utf-8');
        if(empty($id)){
            $this->redirect ( '/error/error404?error=报价信息获取失败' );
            die();
        }
        $quote = new QuotationModel();
        //这边检查id是否存在
        if($quote->getQuoteByInquireId("id = '{$id}' and status='{$_suppliyStateCfg['up']}'")){
            //这边修改状态
            $data = array(
                "update_time"=>$this->_nowtime,
                "update_uid"=>$this->_logininfo['id'],
                "status"=>$_suppliyStateCfg['cancel'],
                "remark"=>"我收到的报价，求购商自己取消报价单",
            );
            $quote->setQuoteStatusByWhere("id = '{$id}'",$data);
            $this->redirect ( '/user/quotelist' );
            die();
        }else{
            $this->redirect ( '/error/error404?error=报价信息不存在' );
            die();
        }
        die();
    }

    //我的信息
    public function userinfoAction(){
        global $_getAreaNames;
        $this->_title ="我的信息";
        $this->_keywords = '我的信息';
        $this->_description = '我的信息';
        $this->_view->title = $this->_title;
        $this->_view->keywords = $this->_keywords;
        $this->_view->description = $this->_description;
        $kd=empty($_GET['kd']) ? '' :htmlspecialchars(trim($_GET['kd']) , ENT_QUOTES ,'utf-8');
        $index = new IndexModel();
        $cate = $index->getCateInfo("cate",'','');
        $user = new UserModel();
        $userinfo = $user->getListById($this->_logininfo['id']);
        $this->_view->cate = $cate;
        $this->_view->kd = $kd;
        $this->_view->userinfo = $userinfo;
        $this->_view->_getAreaNames = $_getAreaNames;
    }







#################################公司信息开始################################
public function userCompanyAction(){
    global $_getAreaNames,$_getAreaname;
    $this->_title ="公司信息";
    $this->_keywords = '公司信息';
    $this->_description = '公司信息';
    $this->_view->title = $this->_title;
    $this->_view->keywords = $this->_keywords;
    $this->_view->description = $this->_description;
    $user = new UserModel();
    $usersinvoice=$user->getUserSinvoiceByUid($this->_logininfo['id']);
    $this->_view->invoice=$usersinvoice;
    $index = new IndexModel();
    $cate = $index->getCateInfo("cate",'','');
    $kd=empty($_GET['kd']) ? '' :htmlspecialchars(trim($_GET['kd']) , ENT_QUOTES ,'utf-8');
    $this->_view->cate = $cate;
    $this->_view->kd = $kd;
    $this->_view->_getAreaNames = $_getAreaNames;
    $this->_view->province = $_getAreaname;
}
    //地区联动
    public function areaAction(){
        if($this->_request->isPost()) {
            $Post = $this->_request->getPost();
            $province_id = !empty($Post['province_id']) ? trim(htmlspecialchars($Post['province_id'], ENT_QUOTES, "UTF-8")) : '';
            $city_id = !empty($Post['city_id']) ? trim(htmlspecialchars($Post['city_id'], ENT_QUOTES, "UTF-8")) : '';
            if ($province_id) {
                $city_data = $this->_db_handler->fetchAll("select region_id as city_id,region_name as city_name from region where region_pid={$province_id}");
                echo json_encode($city_data);
                die();
            }
            if ($city_id) {
                $area_data = $this->_db_handler->fetchAll("select region_id as area_id,region_name as area_name from region where region_pid={$city_id}");
                echo json_encode($area_data);
                die();
            }
        }
    }
//根据公司id查询公司信息
    public function getCompanyAction(){
        if($this->_request->isPost()){
            $Post = $this->_request->getPost();
            $comid=!empty($Post['id']) ? trim(htmlspecialchars($Post['id'], ENT_QUOTES, "UTF-8")) : '';
            $user=new UserModel();
            $comdata=$user->getUserSinvoiceByid($comid);
            $area=$user->getArea($comid);
            if($comdata && $area){
                $data=array_merge($comdata,$area);
            }
            else{
                $data=array(
                    'code'=>"0",
                    'msg'=>"没有数据"
                );
            }
            echo json_encode($data);

            die();
        }
    }
    //添加公司
    //这边添加的公司即为默认公司
    public function addBusinessAction(){
        if($this->_request->isPost()){
            $business=array();
            $business['user_id']=$this->_logininfo['id'];
            $business['business_name'] = empty($_POST['business_name']) ? '' : htmlspecialchars($_POST['business_name'], ENT_QUOTES, 'UTF-8');
            $business['create_uid']=$this->_logininfo['id'];
            $business['create_time']=date ( "Y-m-d H:i:s" ,strtotime('now'));
            $user = new UserModel();
            $isExistBusiness = $user->isExistBusinessByRow('business_name', $_POST['business_name']);
            if (!empty($isExistBusiness)) {
                $arr = array(
                    'code' => 6,
                    'msg' => '该公司名称已经存在'
                );
                echo json_encode($arr);
                die();
            }

            if(empty($business['business_name'])){
                $arr=array(
                    'code' => 5,
                    'msg' => "公司名称不能为空"
                );
                echo json_encode($arr);
                die();
            }else{
                //这边干一件事，把这个人的其他公司都设置为不是默认的
               $this->_db_handler->update("users_invoice",array('is_default'=>'N','update_time'=>date("Y-m-d H:i:s")),"user_id = '{$business['user_id']}'");
                    $business['is_default'] = 'Y';
                    $id = $this->_db_handler->insert('users_invoice', $business);
                    if ($id) {
                        $arr = array(
                            'code' => 3,
                            'msg' => "添加成功!"
                        );
                        echo json_encode($arr);
                        die();
                    } else {
                        $arr = array(
                            'code' => 4,
                            'msg' => "添加失败!"
                        );
                        echo json_encode($arr);
                        die();
                    }
            }
        }
        die();
    }
    //修改公司
    public function updateByIdAction(){
        if($this->_request->isPost()){
            $id = empty($_POST['id']) ? '' : htmlspecialchars($_POST['id'], ENT_QUOTES, 'UTF-8');//公司id
            $field['business_phone'] = empty($_POST['business_phone']) ? '' : htmlspecialchars($_POST['business_phone'], ENT_QUOTES, 'UTF-8');
            $field['main_variety'] = empty($_POST['main_variety']) ? '' : htmlspecialchars($_POST['main_variety'], ENT_QUOTES, 'UTF-8');
            $field['contacts'] = empty($_POST['contacts']) ? '' : htmlspecialchars($_POST['contacts'], ENT_QUOTES, 'UTF-8');
            $field['mobile'] = empty($_POST['mobile']) ? '' : htmlspecialchars($_POST['mobile'], ENT_QUOTES, 'UTF-8');
            $field['phone'] = empty($_POST['phone']) ? '' : htmlspecialchars($_POST['phone'], ENT_QUOTES, 'UTF-8');
            $field['qq'] = empty($_POST['qq']) ? '' : htmlspecialchars($_POST['qq'], ENT_QUOTES, 'UTF-8');
            $field['business_addr'] = empty($_POST['business_addr']) ? '' : htmlspecialchars($_POST['business_addr'], ENT_QUOTES, 'UTF-8');
            $field['business_province_id'] = empty($_POST['business_province_id']) ? '' : htmlspecialchars($_POST['business_province_id'], ENT_QUOTES, 'UTF-8');
            $field['business_city_id'] = empty($_POST['business_city_id']) ? '' : htmlspecialchars($_POST['business_city_id'], ENT_QUOTES, 'UTF-8');
            $field['business_area_id'] = empty($_POST['business_area_id']) ? '' : htmlspecialchars($_POST['business_area_id'], ENT_QUOTES, 'UTF-8');
            $field['is_default'] = empty($_POST['is_default']) ? '' : htmlspecialchars($_POST['is_default'], ENT_QUOTES, 'UTF-8');

            if(empty($field['is_default'])){
                $field['is_default']="N";

                //然而这边当这个值取消默认，那去查询这个公司是否存在默认的公司
                $is_default =  $this->_db_handler->fetchOne("select count(*) from users_invoice where user_id = '{$this->_logininfo['id']}' and is_default = 'Y' and id <>'{$id}'");

                if($is_default<1){
                    $data=array(
                        'code' => 99,
                        'msg' =>'请至少设置一个为默认的公司'
                    );
                    echo json_encode($data);
                    die();
                }

            }





            //这边的目的是先把这个人的公司全部设置为不是默认
            if($field['is_default']){
                $default=array(
                    'is_default'=>'N'
                );
                $this->_db_handler->update("users_invoice", $default, "user_id= {$this->_logininfo['id']}");
            }

            if(empty($field['main_variety'])){
                $data=array(
                    'code' => 2,
                    'msg' =>'主营产品不能为空'
                );
                echo json_encode($data);
                die();
            }
            if(empty($field['contacts'])){
                $data=array(
                    'code' => 3,
                    'msg' => '联系人不能为空'
                );
                echo json_encode($data);
                die();
            }
            if (empty($field['mobile'])) {
                $data = array(
                    'code' => 4,
                    'msg' => '请填写手机号码!'
                );
                echo json_encode($data);
                die();
            }
            if(strlen($field['mobile'])!==11){
                $data = array(
                    'code'=> 5,
                    'msg' => '请填写正确的手机号码!'
                );
                echo json_encode($data);
                die();
            }
            if(!preg_match("/^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/",$field['mobile'])){
                $data = array(
                    'code'=> 6,
                    'msg' => '请填写正确的手机号码!'
                );
                echo json_encode($data);
                die();
            }
            $user=new UserModel();

            $field=array(
                'business_phone'=>$field['business_phone'],
                'main_variety'=>$field['main_variety'],
                'contacts'=>$field['contacts'],
                'mobile'=>$field['mobile'],
                'phone'=>$field['phone'],
                'qq'=>$field['qq'],
                'business_addr'=>$field['business_addr'],
                'update_uid'=> $this->_logininfo['id'],
                'update_time'=>date ( "Y-m-d H:i:s" ),
                'business_province_id'=>$field['business_province_id'],
                'business_city_id'=>$field['business_city_id'],
                'business_area_id'=>$field['business_area_id'],
                'is_default'=>$field['is_default']
            );
            $update=$user->updateInvoiceById($id, $field);
            if($update){
                $data = array(
                    'code' => 1,
                    'msg' => '修改成功'
                );
                echo json_encode($data);
                die();
            }
            else{
                $data = array(
                    'code' => 7,
                    'msg' => '修改失败'
                );
                echo json_encode($data);
                die();
            }
            die("11111111111");
        }
    }
#################################公司信息结束################################
    //公司认证

    public function businesslicensesAction(){
        global $_getAreaNames;
        $this->_title ="公司认证";
        $this->_keywords = '公司认证';
        $this->_description = '公司认证';
        $this->_view->title = $this->_title;
        $this->_view->keywords = $this->_keywords;
        $this->_view->description = $this->_description;

        $kd=empty($_GET['kd']) ? '' :htmlspecialchars(trim($_GET['kd']) , ENT_QUOTES ,'utf-8');

        $index = new IndexModel();
        $cate = $index->getCateInfo("cate",'','');
        $Business = new UsersinvoiceModel();
        $Business_info = $Business->getUserInfoById($this->_logininfo['id'],"","a");//查询所有的公司--fetchall
        $Business_info_default = $Business->getUserInfoById($this->_logininfo['id'],"Y","r");//查询默认的公司

        if(!empty($Business_info_default['certificate'])){
            $certificate =   json_decode($Business_info_default['certificate'],true);
        }else{
            $certificate = array(
                'b_certificate'=>'',
                'tr_certificate'=>'',
                'oc_certificate'=>'',
            );
        }

        $this->_view->Business_info = $Business_info;
        $this->_view->Business_info_default = $Business_info_default;

        $this->_view->cate = $cate;
        $this->_view->kd = $kd;

        $this->_view->certificate = $certificate;
        $this->_view->_getAreaNames = $_getAreaNames;
    }
    //保存图片
    public function savelicensesAction(){
        if($this->_request->isPost()) {
            $Post = $this->_request->getPost();
            $business_id = !empty($Post['business_id']) ? (int)htmlspecialchars(trim($Post['business_id']), ENT_QUOTES, "UTF-8") : '';
            if(empty($business_id)){
                Tools::dieJs('alert("公司不正确");history.go(-1);');
            }
            //这边检查公司di是否是当前的用户的
            $business = new UsersinvoiceModel();
           $userinvoice =  $business->getUserBusinessyName("user_id= '{$this->_logininfo['id']}' and id='{$business_id}'");
            if(empty($userinvoice)){
                Tools::dieJs('alert("公司名称和当前用户不同，请正确操作");history.go(-1);');
            }

            if(!empty($userinvoice['certificate'])){
                $certificate =   json_decode($userinvoice['certificate'],true);
            }

            if(!empty($_FILES)){
                if(!empty($_FILES['b_certificate']['name'])){
                    $upload = new  Fileupload();
                    $upload->set("path","./upload/liceses/");
                    $upload->upload("b_certificate");
                    $filename =$upload->getFileName();
                    $uploadFile['b_certificate'] ="liceses/".$filename;
                    $Image = new  Image($upload->getFilepath());
                    $Image->thumb($filename,300,150,'b_');
                }else{
                    $uploadFile['b_certificate']=$certificate['b_certificate'];
                }

                if(!empty($_FILES['tr_certificate']['name'])){
                    $upload = new  Fileupload();
                    $upload->set("path","./upload/liceses/");
                    $upload->upload("tr_certificate");
                    $filename =$upload->getFileName();
                    $uploadFile['tr_certificate'] ="liceses/".$filename;
                    $Image = new  Image($upload->getFilepath());
                    $Image->thumb($filename,300,150,'tr_');
                }else{
                    $uploadFile['tr_certificate']=$certificate['tr_certificate'];
                }

                if(!empty($_FILES['oc_certificate']['name'])){
                    $upload = new  Fileupload();
                    $upload->set("path","./upload/liceses/");
                    $upload->upload("oc_certificate");
                    $filename =$upload->getFileName();
                    $uploadFile['oc_certificate'] ="liceses/".$filename;
                    $Image = new  Image($upload->getFilepath());
                    $Image->thumb($filename,300,150,'oc_');
                }else{
                    $uploadFile['oc_certificate']=$certificate['oc_certificate'];
                }
                $certificate_up = json_encode($uploadFile);
            }else{
                $certificate_up =   $userinvoice['certificate'];
            }

            $data = array(
                'update_time'=>$this->_nowtime,
                'update_uid'=>$this->_logininfo['id'],
                'certificate'=>$certificate_up
            );

            //保存数据
            $res = $business->updateById($business_id,$data);
            if($res && $res>0){
                $this->redirect('/user/businesslicenses');
                die();
            }else{
                $this->redirect('/error/error404?error=操作失败');
                die();
            }
        }
    }

    //收货地址
    public function receiveaddressAction(){
        $this->_title ="收货地址";
        $this->_keywords = '收货地址';
        $this->_description = '收货地址';
        $this->_view->title = $this->_title;
        $this->_view->keywords = $this->_keywords;
        $this->_view->description = $this->_description;

        $kd=empty($_GET['kd']) ? '' :htmlspecialchars(trim($_GET['kd']) , ENT_QUOTES ,'utf-8');

        $index = new IndexModel();
        $cate = $index->getCateInfo("cate",'','');


        $this->_view->cate = $cate;
        $this->_view->kd = $kd;
    }

    //修改密码
    public function changepasswordAction(){
        global $_getAreaNames;
        $this->_title ="修改密码";
        $this->_keywords = '修改密码';
        $this->_description = '修改密码';
        $this->_view->title = $this->_title;
        $this->_view->keywords = $this->_keywords;
        $this->_view->description = $this->_description;

        $kd=empty($_GET['kd']) ? '' :htmlspecialchars(trim($_GET['kd']) , ENT_QUOTES ,'utf-8');

        $index = new IndexModel();
        $cate = $index->getCateInfo("cate",'','');


        $this->_view->cate = $cate;
        $this->_view->kd = $kd;
        $this->_view->_getAreaNames = $_getAreaNames;
    }


    //下载报价模板

    public function downquoteAction(){
        ob_clean();
        $nowtime = date('Y-m-d H:i:s' ,strtotime('now'));
        $type = $this->_request->getParam("type",'');
        //根据$type 生成不同类型的默认值
        $Business = new UsersinvoiceModel();
        $Business_info_default = $Business->getUserInfoById($this->_logininfo['id'],"Y","r");//这是查询默认信息，展示公司的基本信息

        $quote_list[0] = array(
            'business_name'=>$Business_info_default['business_name'],
            'cate_name'=>'LDPE',
            'product_name'=>'7000F',
            'brand_name'=>'伊朗石化',
            'service_name'=>'现货',
            'price'=>'10000',
            'currency_name'=>($type=='rmb')?'人民币':'美元',
            'settlement'=>($type=='rmb')?'':'L/C-A/S',
            'num'=>'100',
            'delivery_method'=>'自提',
            'areaname'=>'上海',
//            'delivery_area'=>'上海宝山',
            'delivery_time'=>'2015-12-12',
        );

        $excelHeadData = array (
            "business_name" => "供应商名称",
            "cate_name" => "品种",
            "product_name" => "牌号",
            "brand_name" => "厂商",
            "service_name" => "交易类型(现货，期货)",
            "price" => "价格(元/吨)",
            "currency_name" => "币种(人民币，美元)",
            "settlement" => "结算方式(L/C-A/S,L/C-60天,L/C-90天,L/C-120天,T/T)",
            "num" => "数量(吨)",
            "delivery_method" => "提货方式(自提，配送)",
            "areaname" => "交货地点(请正确填写城市名：如宁波市，上海市，浙江省，但不能填：浙江宁波两个名称混合,更不能填写其他不符合地区的名称，否则报价不显示)",
//            "delivery_area" => "交货地点",
            "delivery_time" => "交货时间(请表格已文本形式填写)",
        );

        $phpExcel = new PHPExcel ();
        // 读取模版
        $saleFile = "excelModel/quote_rmb.xls";
        $PHPReader = new PHPExcel_Reader_Excel5 ();
        if ($PHPReader->canRead ( $saleFile )) {
            $phpExcel = $PHPReader->load ( $saleFile );
        }
        // 定义输出的表名
        $filename = "供应商报价单" . date ( "YmdHis" ) . ".xls";
        // 设置字体颜色为红色
        $styleFontColorRed = array (
            "font" => array (
                "color" => array (
                    "argb" => "FFFF0000"
                )
            )
        );
        // 设置表头
        $key = ord ( "A" );
        foreach ( $excelHeadData as $key1 => $value ) {
            $colum = chr ( $key );
            $phpExcel->setActiveSheetIndex ( 0 )->setCellValue ( $colum . '1', $value );
            $key += 1;
        }
        // 循环读入数据的内容
        $column = 2;
        foreach ( $quote_list as $key1 => $val) {
            // 格式化存入的内容
            $excelData = array (
                "business_name" => $val['business_name'],
                "cate_name" => $val['cate_name'],
                "product_name" => $val['product_name'],
                "brand_name" => $val['brand_name'],
                "service_name" => $val['service_name'],
                "price" => $val['price'],
                "currency_name" => $val['currency_name'],
                "settlement" => $val['settlement'],
                "num" => $val['num'],
                "delivery_method" => $val['delivery_method'],
                "areaname" => $val['areaname'],
//                "delivery_area" => $val['delivery_area'],
                "delivery_time" => $val['delivery_time'],
            ) ;
            // 设置内容数据
            $key = ord ( "A" );
            foreach ( $excelData as $key2 => $value2 ) {
                $colum = chr ( $key );
                //C行全部设置为文本型的数据
                if($colum =="C"){
                    $phpExcel->getActiveSheet()->setCellValueExplicit('C'.$column,$value2,PHPExcel_Cell_DataType::TYPE_STRING);
                }else{
                    $phpExcel->setActiveSheetIndex ( 0 )->setCellValue ( $colum . $column, $value2 );
                }
                // 状态为取消的时候设置为红色
//                if ("已取消" === $value2) {
//                    $phpExcel->getActiveSheet ()->getStyle ( $colum . $column )->applyFromArray ( $styleFontColorRed );
//                }
                $key += 1;
            }
            $column ++;
        }

        // 设置筛选 筛选一定是一个范围 如果是每列筛选 就筛选全表 从A1 到最后一列一行的结尾
        $phpExcel->getActiveSheet ()->setAutoFilter ( 'A1:T' . $column );
        $filename = iconv ( "utf-8", "gb2312", $filename );
        $obj_Writer = PHPExcel_IOFactory::createWriter ( $phpExcel, 'Excel5' );

        header ( "Content-Type: application/force-download" );
        header ( "Content-Type: application/octet-stream" );
        header ( "Content-Type: application/download" );
        header ( 'Content-Disposition:inline;filename="' . $filename . '"' );
        header ( "Content-Transfer-Encoding: binary" );
        header ( "Last-Modified: " . gmdate ( "D, d M Y H:i:s" ) . " GMT" );
        header ( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
        header ( "Pragma: no-cache" );
        $obj_Writer->save ( 'php://output' );
        exit ();



    }




    ###################################################################################################

    //关于我们
    public function aboutAction(){
        $this->_title ="关于我们";
        $this->_view->title = $this->_title;
    }

    //设置中心
    public function setupAction(){
        $this->_title ="设置中心";
        $this->_view->title = $this->_title;
    }


}